-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgenerate.js
169 lines (146 loc) · 5.07 KB
/
generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var fs = require('fs')
var fse = require('fs-extra')
var mustache = require('mustache')
var path = require('path')
var yaml = require('yaml')
var codegen = require('elm-codegen')
const { execSync } = require("child_process");
const isDir = fileName => {
try {
return fs.lstatSync(fileName).isDirectory() || fs.lstatSync(fileName).isSymbolicLink();
} catch(e) {
if (e.code === 'ENOENT') {
return false
}
}
};
const pluginsFolder = './plugins'
const templatesFolder = './plugin_templates'
const genFolder = './generated'
const genPluginsFolder = path.join(genFolder, pluginsFolder)
const langFolder = './lang'
const themeFolder = './theme'
const publicFolder = './public'
const genPublicFolder = path.join(genFolder, publicFolder)
const genLangFolder = path.join(genFolder, publicFolder, langFolder)
fse.copySync(publicFolder, genPublicFolder, {recursive: true})
fse.copySync(langFolder, genLangFolder, {recursive: true})
console.log('Installing plugins:')
let plugins = fs.readdirSync(pluginsFolder)
.filter(fileName => isDir(path.join(pluginsFolder, fileName)))
plugins.sort((a, b) => {
// Check if strings end with '_preview'
const aEndsWithPreview = a.endsWith("_preview");
const bEndsWithPreview = b.endsWith("_preview");
// If both or neither strings end with '_preview', sort them lexicographically
if (aEndsWithPreview === bEndsWithPreview) {
return a.localeCompare(b);
}
// If only one string ends with '_preview', place it after the other
return aEndsWithPreview ? 1 : -1;
});
plugins = plugins.map(plugin => {
console.log(plugin)
return {
name : plugin,
package : plugin.charAt(0).toUpperCase() + plugin.slice(1)
}
})
if(plugins.length === 0) {
console.log('No plugins found')
} else {
plugins[plugins.length - 1].last = true
}
console.log("")
const transform = (folder) => {
fs.readdirSync(path.join(templatesFolder, folder))
.map(fileName => {
if(isDir(path.join(templatesFolder, folder, fileName))) {
transform(path.join(folder, fileName))
return
}
if (path.extname(fileName) !== '.mustache') return
const file = fs.readFileSync(path.join(templatesFolder, folder, fileName), 'utf8')
const newFileName = path.join(genPluginsFolder, folder, fileName.replace('.mustache', ''))
console.log('Generating ', newFileName)
const gen = mustache.render(file, {plugins})
const pf = path.join(genPluginsFolder, folder)
if(!isDir(pf)) {
fs.mkdirSync(pf, {recursive: true})
}
fs.writeFileSync(newFileName, gen)
})
}
const appendLang = (plugin) => {
console.log('Merge translation files for', plugin)
const pluginLangFolder = path.join(pluginsFolder, plugin, langFolder)
fs.readdirSync(pluginLangFolder)
.map(fileName => {
let coreLangFilename = path.join(langFolder, fileName)
let genLangFilename = path.join(genLangFolder, fileName)
let pluginLangFilename = path.join(pluginLangFolder, fileName)
if(!fs.existsSync(coreLangFilename)) {
console.err(`Ignoring ${pluginLangFilename}.`)
return
}
let strings = yaml.parse(fs.readFileSync(genLangFilename, 'utf8'))
let pluginStrings = yaml.parse(fs.readFileSync(pluginLangFilename, 'utf8'))
strings = yaml.stringify({...strings, ...pluginStrings})
fs.writeFileSync(genLangFilename, strings, {flag: 'w+'})
console.log('Merged', fileName)
})
}
const copyPublic = (plugin) => {
const pluginPublicFolder = path.join(pluginsFolder, plugin, publicFolder)
if (!fs.existsSync(pluginPublicFolder)) return
fse.copySync(pluginPublicFolder, genPublicFolder)
console.log('Copied public folder', pluginPublicFolder)
}
const makeTheme = (plugin) => {
const pluginThemeFile = path.join(pluginsFolder, plugin, themeFolder, 'figma.json')
console.log("Making theme from " + pluginThemeFile)
if (!fs.existsSync(pluginThemeFile)) return
/*
codegen.run("Generate.elm", {
debug: true,
output: "theme",
flags: JSON.parse(fs.readFileSync(pluginThemeFile, 'utf8')),
cwd: "./codegen",
})
*/
try {
execSync(`make PLUGIN_NAME=${plugin} plugin-theme`)
} catch(e) {
console.log(e.message)
}
}
transform('./')
for(const plugin in plugins) {
appendLang(plugins[plugin].name)
copyPublic(plugins[plugin].name)
makeTheme(plugins[plugin].name)
}
const elmJson = JSON.parse(fs.readFileSync('./elm.json.base'))
plugins.forEach(plugin => {
const p = path.join(pluginsFolder, plugin.name, 'src')
if(elmJson['source-directories'].indexOf(p) === -1) {
elmJson['source-directories'].push(p)
}
})
fs.writeFileSync('./elm.json', JSON.stringify(elmJson, null, 4))
console.log("\nUpdated src directories in elm.json")
plugins.forEach(plugin => {
const deps = fs.readFileSync(path.join(pluginsFolder, plugin.name, 'dependencies.txt'), 'utf8')
deps.split("\n").forEach(dep => {
if(!dep) return
let cmd = 'yes | elm install ' + dep
console.log(cmd)
try {
console.log(execSync(cmd).toString('utf-8'))
} catch(e) {
console.error('ERROR:')
console.error(e.message)
process.exit(1)
}
})
})