-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvasit.js
169 lines (143 loc) · 5.55 KB
/
canvasit.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
/// <reference path="./typedef.js" />
const { resolve, relative, parse, dirname, } = require('path')
const { emptyDirPartial } = require('./lib/utils')
const { fragmentMapper } = require('./lib/blueprint/fragmentMapper')
const { HookHelpers } = require('./lib/blueprint/hookHelpers')
const { fileWalker } = require('./lib/utils/fileWalker')
const { patchFile } = require('./lib/filePatcher')
const {
existsSync,
readFileSync,
removeSync,
ensureDirSync,
mkdtempSync,
rmdirSync,
readdirSync
} = require('fs-extra')
const { watch } = require('chokidar')
const { configent } = require('configent')
const { spawn, execSync } = require('child_process')
const { populateConfigs } = require('./lib/blueprint/populateConfig')
const defaults = require('./default.config')
/**
*
* @param {string[]|string} paths
* @param {string} output
* @param {Object.<string, any>} input
*/
async function merge(paths = [], output, input = {}) {
const options = configent(defaults, input)
output = output || options.output || 'output'
output = resolve(output)
paths = typeof paths === 'string' ? paths.split(',') : paths
if (options.hooks.init)
await options.hooks.init({ options, paths })
paths.unshift(...options.include)
if(!paths.length)
throw new Error('No paths were specified')
const fragments = []
.concat(...paths.map(fragmentMapper(options.basepath)))
.filter(Boolean)
const _run = async () => {
return await run(fragments, output, options)
}
if (options.watch) {
const watcher = watch(fragments.map(f => f.path))
.on('ready', () => {
console.log('watching', fragments.map(f => f.name))
watcher.on('all', async (event, path) => {
const eventMap = {
'add': `added`,
'change': `changed`,
'unlink': 'deleted'
}
const msg = eventMap[event]
if (msg) {
Object.keys(require.cache).forEach(key => delete require.cache[key])
console.log(`[canvasit] Rebuilding (${msg}: "${path}")`)
if (event === 'unlink') {
const parentDirOfUnlik = paths.find(_path => path.startsWith(_path))
const relativePathToUnlik = relative(resolve(parentDirOfUnlik, 'template'), path)
const outputFile = resolve(output, relativePathToUnlik)
removeSync(outputFile)
}
await _run()
}
})
})
}
emptyDirPartial(output, options.ignore)
const result = await _run()
if (options.exec)
runExec(options.exec, output)
return result
}
function runExec(exec, output) {
const [cmd, ...params] = exec.split(' ')
spawn(cmd, params, {
cwd: output,
stdio: ['inherit', 'inherit', 'inherit'],
shell: true,
})
}
async function run(fragments, output, options) {
const basename = parse(output).base
const tmpPath = '.canvasit-temp'
ensureDirSync(tmpPath)
const tmpOutput = mkdtempSync(`${resolve(tmpPath, basename)}-`)
const folders = fragments.map(f => f.template)
const configs = {}
const imports = {}
const ctx = { configs, imports, fragments, output: tmpOutput, folders }
const handleEvent = createEventHandler(ctx)
// map all fragment imports to imports
Object.assign(imports, ...fragments.map(f => f.blueprint && f.blueprint.imports))
// create configs
await handleEvent('beforeConfig')
populateConfigs(fragments, configs)
await handleEvent('afterConfig')
// copy non fragment files to tmp
await handleEvent('beforeCopy')
await fileWalker(folders, file => {
if (!file.filepath.match(/.+\.fragment\.(j|t)s/)) {
const dest = resolve(tmpOutput, file.relativePath)
ensureDirSync(dirname(dest))
require('fs').copyFileSync(file.filepath, dest)
}
}, options.ignore)
await handleEvent('afterCopy')
await handleEvent('beforePatch')
await fileWalker(tmpOutput, async file => await patchFile(file.filepath, folders, tmpOutput, configs, imports), options.ignore)
await handleEvent('afterPatch')
if (options.prettier){
const plugins = options.prettierPlugins.map(p => `--plugin ${p}`).join(' ')
execSync(`npx prettier "${tmpOutput}/**/*.{js,svelte}" --write --single-quote --no-semi ${plugins}`)
}
// copy tmp to actual folder
await fileWalker(tmpOutput, file => {
const dest = resolve(output, file.relativePath)
if (!existsSync(dest) || readFileSync(dest, 'utf8') !== file.content) {
ensureDirSync(dirname(dest))
require('fs').copyFileSync(file.filepath, dest)
}
}, options.ignore)
removeSync(tmpOutput)
if (!readdirSync(tmpPath).length)
rmdirSync(tmpPath)
console.log('Created template in', output)
return { configs, fragments }
}
function createEventHandler(ctx) {
return async function handleEvent(eventName) {
for (let { blueprint } of ctx.fragments) {
if (blueprint.hooks && blueprint.hooks[eventName]) {
const callback = blueprint.hooks[eventName]
const helpers = new HookHelpers({ ...ctx, blueprint })
await callback(helpers)
}
}
}
}
/** @type {Blueprint} */
let Blueprint
module.exports = { merge, Blueprint }