forked from AnandChowdhary/update-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.ts
397 lines (338 loc) · 11.2 KB
/
update.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import { execSync } from 'child_process'
import { join } from 'path'
import fg from 'fast-glob'
import fsExtra from 'fs-extra'
import c from 'ansi-colors'
import equal from 'deep-equal'
import type { Source, Config, Package, Changes, SourceChanges } from './types.js'
import semver from 'semver'
const { readJson, copyFile, remove, ensureFile, writeFile, stat } = fsExtra
/** The name of the file in which we store a record of the changes we've made. */
const CHANGES_FILE = '.update-template-changes.json'
type UpdateMode = 'replace' | 'add' | 'delete'
export const update = async(): Promise<number> => {
const previousChanges = await loadPreviousChanges()
const args = process.argv.slice(2)
let mode: UpdateMode = 'replace'
if (args[0] === '-a') {
mode = 'add'
args.shift()
} else if (args[0] === '-d') {
mode = 'delete'
args.shift()
}
const previousPaths = Object.keys(previousChanges.sources)
let sourcePaths: string[]
if (mode === 'add') {
sourcePaths = [...previousPaths]
for (const toAdd of args) {
if (sourcePaths.indexOf(toAdd) === -1) {
sourcePaths.push(toAdd)
}
}
} else if (mode === 'delete') {
sourcePaths = [...previousPaths]
for (const toDelete of args) {
while (true) {
const index = sourcePaths.indexOf(toDelete)
if (index !== -1) {
sourcePaths.splice(index, 1)
} else {
break
}
}
}
} else if (mode === 'replace') {
if (args.length === 0) {
/* If no paths are specified, update the templates already applied */
sourcePaths = [...previousPaths]
} else {
sourcePaths = args
}
} else {
throw new Error(`Invalid mode: ${mode}`)
}
const sources: Source[] = []
/* Load sources */
for (const sourcePath of sourcePaths) {
try {
const source = await findTemplateSource(sourcePath)
if (!source) {
throw new Error(`Couldn't load source: ${sourcePath}`)
}
sources.push(source)
} catch (error) {
console.error(c.red.bold(`${sourcePath}: ${error instanceof Error ? error.message : error}`))
return 1
}
}
if (sources.length === 0) {
throw new Error('Provide source repositories or paths')
}
const localPackageJson: Package = await readJson(join('.', 'package.json'))
let workingPackageJson: Package = { ...localPackageJson }
const changes: Changes = {
sources: {},
}
/* Remove any changes from templates no longer used */
for (const sourceName of Object.keys(previousChanges.sources)) {
if (!sources.find(s => s.name === sourceName)) {
console.log(c.red.bold(`${sourceName} (removing template)`))
const previousSourceChanges = previousChanges.sources[sourceName]!
workingPackageJson = await undoPreviousTemplateChanges(workingPackageJson, sourceName, previousSourceChanges)
}
}
for (const source of sources) {
console.log(c.bold.green(`${source.name}`))
const config = source.config
const sourceChanges: SourceChanges = {
files: [],
package: {},
}
changes.sources[source.name] = sourceChanges
const previousSourceChanges = previousChanges.sources[source.name]
const previousFiles = previousSourceChanges ? previousSourceChanges.files : []
const files = (
await fg((config.files || []).map((glob) => `${source.path}/${glob}`))
).map((file) => file.substring(source.path.length))
for await (const file of files) {
await ensureFile(join('.', file))
const dest = join('.', file)
console.log(c.bold.blue(` Copying: ${dest}`))
await copyFile(join('.', source.path, file), dest)
sourceChanges.files.push(file)
const foundPreviousFile = previousFiles.indexOf(file)
if (foundPreviousFile !== -1) {
previousFiles.splice(foundPreviousFile, 1)
}
}
/* Remove defunct files from template */
if (previousFiles.length > 0) {
for (const file of previousFiles) {
const path = join('.', file)
console.log(c.blue.bold(` Removing: ${path} (removed from template)`))
await remove(path)
}
}
const templatePackageJson = await readJson(
join('.', source.path, 'package.json'),
)
workingPackageJson = applyPackageChanges(workingPackageJson, templatePackageJson, config, previousSourceChanges, sourceChanges)
config.removeFiles = config.removeFiles || []
config.removeFiles = [...config.removeFiles, '.templaterc.json']
const filesToDelete = await fg(config.removeFiles)
for await (const file of filesToDelete) {
const path = join('.', file)
console.log(c.blue.bold(` Removing: ${path}`))
await remove(path)
}
/* Remove any remaining changes, that are no longer made by templates */
if (previousSourceChanges) {
workingPackageJson = await undoPreviousTemplateChanges(workingPackageJson, source.name, previousSourceChanges)
}
if (source.temporary) {
await remove(join('.', source.path))
}
}
/* Update package.json */
if (!equal(localPackageJson, workingPackageJson)) {
await writeFile(
join('.', 'package.json'),
JSON.stringify(workingPackageJson, null, 2) + '\n',
)
}
/* Write changes file */
await writeFile(
join('.', CHANGES_FILE),
JSON.stringify(changes, null, 2) + '\n',
)
return 0
}
async function cloneRepo(repoUrl: string): Promise<Source | undefined> {
// If this is running in a Github Actions workflow, we know the repo name
const [owner, repo] = (process.env.GITHUB_REPOSITORY || '').split('/')
if (owner && repo)
if (repoUrl === `https://github.com/${owner}/${repo}`) {
console.log('Skipping updating same repo')
return undefined
}
const tempDir = `tempDir_${Math.random().toString(32).split('.')[1]}`
execSync(`git clone ${repoUrl} ${tempDir}`)
let config: Config = {}
try {
config = await readJson(join('.', tempDir, '.templaterc.json'))
} catch (error) {
console.log(`${repoUrl}: .templaterc.json config file: ${error instanceof Error ? error.message : error}`)
}
return {
name: repoUrl,
path: tempDir,
temporary: true,
config,
}
}
async function findTemplateSource(urlOrPath: string): Promise<Source | undefined> {
if (urlOrPath.includes('//')) {
return cloneRepo(urlOrPath)
}
const statResult = await stat(urlOrPath)
if (statResult.isDirectory()) {
let config: Config = {}
try {
config = await readJson(join('.', urlOrPath, '.templaterc.json'))
} catch (error) {
console.log(`${urlOrPath}: .templaterc.json config file: ${error instanceof Error ? error.message : error}`)
}
return {
name: urlOrPath,
path: urlOrPath,
temporary: false,
config,
}
}
}
function applyPackageChanges(target: Package, template: Package, config: Config, previousChanges: SourceChanges | undefined, changes: SourceChanges): Package {
const result: Package = { ...target }
function applyObjectChange(key: string, combine?: CombineFunction) {
if (!template[key]) {
console.log(c.yellow.bold(` Package is missing "${key}"`))
return
}
changes.package[key] = template[key]
result[key] = combineRecords(target[key] as Record<string, string>, template[key] as Record<string, string>, combine)
if (previousChanges && previousChanges.package[key] && template[key]) {
for (const subkey of Object.keys(template[key])) {
delete (previousChanges.package[key] as Record<string, string>)[subkey]
}
}
if (!equal(target[key], result[key])) {
console.log(c.cyan.bold(` Updated: ${key}`))
}
}
if (config.package?.engines) {
applyObjectChange('engines', combineChoosingLatest)
}
if (config.npmDependencies || config.package?.dependencies) {
applyObjectChange('dependencies', combineChoosingLatest)
}
if (config.package?.devDependencies) {
applyObjectChange('devDependencies', combineChoosingLatest)
}
if (config.package?.optionalDependencies) {
applyObjectChange('optionalDependencies', combineChoosingLatest)
}
if (config.package?.peerDependencies) {
applyObjectChange('peerDependencies', combineChoosingLatest)
}
if (config.npmScripts || config.package?.scripts) {
applyObjectChange('scripts')
}
return result
}
function undoPackageChanges(target: Package, template: Package): Package {
const result: Package = { ...target }
function applyObjectChange(key: string) {
result[key] = removeRecords(target[key] as Record<string, string>, template[key] as Record<string, string>)
if (!equal(target[key], result[key])) {
console.log(c.red.bold(` Updated: ${key}`))
}
}
if (template.engines) {
applyObjectChange('engines')
}
if (template.dependencies) {
applyObjectChange('dependencies')
}
if (template.devDependencies) {
applyObjectChange('devDependencies')
}
if (template.optionalDependencies) {
applyObjectChange('optionalDependencies')
}
if (template.peerDependencies) {
applyObjectChange('peerDependencies')
}
if (template.scripts) {
applyObjectChange('scripts')
}
return result
}
type CombineFunction = (existingValue: string | undefined, newValue: string) => string
const DEFAULT_COMBINE: CombineFunction = (existingValue, newValue) => newValue
function combineChoosingLatest(existingValue: string | undefined, newValue: string): string {
if (!existingValue || existingValue === newValue) {
return newValue
}
if (!semver.validRange(existingValue) && semver.validRange(newValue)) {
/* The existing value is not semver while the new is, so we'll retain the existing */
return existingValue
}
if (!semver.validRange(existingValue) || !semver.validRange(newValue)) {
/* Neither are semver, so we'll use the new value as we cannot compare */
return newValue
}
const minExistingVersion = semver.minVersion(existingValue)
const minNewVersion = semver.minVersion(newValue)
if (!minExistingVersion || !minNewVersion) {
/* Neither are semver, so we'll use the new value as we cannot compare */
return newValue
}
if (minNewVersion.compare(minExistingVersion) > 0) {
return newValue
} else {
return existingValue
}
}
function combineRecords(existing: Record<string, string>, template: Record<string, string>, combine: CombineFunction = DEFAULT_COMBINE): Record<string, string> {
if (typeof existing !== 'object') {
if (typeof template !== 'object') {
return existing
} else {
return template
}
} else if (typeof template !== 'object') {
return existing
}
const combined = {
...existing,
}
for (const key of Object.keys(template)) {
combined[key] = combine(combined[key], template[key])
}
const ordered: Record<string, string> = {}
Object.keys(combined)
.sort()
.forEach((key) => (ordered[key] = combined[key]))
return ordered
}
function removeRecords(existing: Record<string, string>, template: Record<string, string>): Record<string, string> {
if (typeof existing !== 'object' || typeof template !== 'object') {
return existing
}
const combined = {
...existing,
}
for (const key of Object.keys(template)) {
delete combined[key]
}
return combined
}
async function loadPreviousChanges(): Promise<Changes> {
try {
return await readJson(join('.', CHANGES_FILE))
} catch {
/* Ignore no previous changes file */
return {
sources: {},
}
}
}
async function undoPreviousTemplateChanges(workingPackageJson: Package, sourcePath: string, sourceChanges: SourceChanges): Promise<Package> {
/* Remove any files from templates no longer used */
for (const file of sourceChanges.files) {
const path = join('.', file)
console.log(c.blue.bold(` Removing: ${path}`))
await remove(path)
}
return undoPackageChanges(workingPackageJson, sourceChanges.package)
}