This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
jam-on.mjs
435 lines (381 loc) · 13.5 KB
/
jam-on.mjs
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* eslint-disable no-console */
import { v4 as uuidv4 } from 'uuid';
import { Command } from 'commander';
import { simpleGit as git } from 'simple-git';
import inquirer from 'inquirer';
import fs from 'fs-extra';
import nunjucks from 'nunjucks';
import validate from 'validate-npm-package-name';
import path from 'node:path';
import { createRequire } from 'node:module';
// Because assetions are still experimental in Node.js,
// we need to construct a require function within this ES module file
var require = createRequire(import.meta.url);
const { version: toolkitPackageJSONVersion } = require('./package.json');
const program = new Command();
nunjucks.configure('.jam-on/core/templates');
function generateToolkitFile(pathToFile, content, message) {
fs.outputFileSync(pathToFile, content);
console.log(message);
}
function newAction(options) {
if (options.keepGit) {
console.log('This will remove the example pages and components');
} else {
console.log(
'This will remove any local Git information, and the example pages and components',
);
}
let hasGit;
let gitIsClean;
try {
fs.statSync('.git');
hasGit = true;
} catch (e) {
hasGit = false;
gitIsClean = true;
}
if (hasGit) {
git().status({}, (error, status) => {
if (error) {
console.log('Error in git().status() command: ', error);
process.exit();
} else {
gitIsClean = status.isClean();
}
});
}
inquirer
.prompt({
type: 'confirm',
name: 'doNew',
default: false,
message: 'Proceed?',
})
.then((answers) => {
if (!answers.doNew) {
console.log('Farewell!');
process.exit();
}
if (gitIsClean && !options.keepGit) {
console.log(
'Local Git repo is clean or does not exist, removing any Git information',
);
fs.removeSync('.git');
} else if (!options.keepGit) {
console.log(
'Your local Git repo has modifications; please ensure the local git repo is clean and unmodified before running this command',
);
console.log('Farewell!');
process.exit();
}
console.log('Creating new project...');
console.log('Removing example files...');
const exampleFilesList = [
'src/_includes/app/components/_example_page_list.njk',
'src/example-pages',
'src/pages-dexemple',
'src/index.njk',
];
exampleFilesList.forEach((filePath) => {
fs.removeSync(filePath);
});
inquirer
.prompt([
{
type: 'input',
name: 'enRoot',
message: 'What is the English-language subfolder for deployment?',
},
{
type: 'input',
name: 'frRoot',
message: 'What is the French-language subfolder for deployment?',
},
{
type: 'input',
name: 'projectName',
message:
'What is the NPM package.json name for this project (use lowercase, hyphens and underscores only)?',
default: 'new-jam-on-project',
validate(userInput) {
const validName = validate(userInput).validForNewPackages;
if (validName) {
return true;
}
return 'Invalid package name';
},
},
{
type: 'input',
name: 'projectDescription',
message: 'What is a short description for this project?',
default: 'New Ontario.ca Jamstack Toolkit project',
},
])
.then((responses) => {
console.log('Creating starter files...');
const newConf = {
assetsDestination: `${responses.enRoot}/assets`,
englishRoot: responses.enRoot,
frenchRoot: responses.frRoot,
projectName: responses.projectName,
projectDescription: responses.projectDescription,
createDate: new Date().toISOString(),
toolkitPackageJSONVersion,
};
generateToolkitFile(
'.jam-on/app/conf.json',
JSON.stringify(newConf),
'Wrote new config file to .jam-on/app/conf.json',
);
const enFileContent = nunjucks.render('en.njk', newConf);
const frFileContent = nunjucks.render('fr.njk', newConf);
const redirectFileContent = nunjucks.render('redirect.njk', newConf);
generateToolkitFile(
`src/${newConf.englishRoot}.njk`,
enFileContent,
`Wrote English-side starter file at src/${newConf.englishRoot}.njk`,
);
generateToolkitFile(
`src/${newConf.frenchRoot}.njk`,
frFileContent,
`Wrote French-side starter file at src/${newConf.frenchRoot}.njk`,
);
generateToolkitFile(
'src/index.njk',
redirectFileContent,
'Wrote root-level redirect file at src/index.njk',
);
const testFileContent = nunjucks.render('test.njk', newConf);
generateToolkitFile(
'test/test.js',
testFileContent,
'Wrote starter test file at test/test.js',
);
const packageFileContent = nunjucks.render('package.njk', newConf);
generateToolkitFile(
'package.json',
packageFileContent,
'Wrote updated NPM package.json file at package.json',
);
fs.renameSync('README.md', 'README.jamstack.md');
console.log('Renamed default README file');
const readmeFileContent = nunjucks.render('README.njk', newConf);
generateToolkitFile(
'README.md',
readmeFileContent,
'Wrote new README file at README.md',
);
const sitemapFileContent = nunjucks.render('sitemap.njk', newConf);
generateToolkitFile(
`src/sitemap.njk`,
sitemapFileContent,
`Wrote sitemap.njk file at src/sitemap.njk`,
);
});
})
.catch((error) => {
console.log(error);
});
}
function updateAction(tagOrBranch, options) {
let ignoreFile;
let ignoreFileContent;
console.log(
`This will replace the 'core' and 'vendor' directories/files of the current project to the versions in Jamstack Toolkit version ${tagOrBranch}`,
);
inquirer
.prompt({
type: 'confirm',
name: 'doUpdate',
default: false,
message: 'Perform the upgrade?',
})
.then((answers) => {
if (!answers.doUpdate) {
console.log('Farewell!');
process.exit();
}
const tmpDir = 'tmp';
const tmpCheckoutDir = `${tmpDir}${path.sep}${uuidv4()}`;
ignoreFile = `${tmpDir}${path.sep}.gitignore`;
ignoreFileContent = '*';
fs.outputFileSync(ignoreFile, ignoreFileContent);
console.log(`Updating to branch/tag: ${tagOrBranch}`);
const jamOnPath = '/jam-on.mjs';
const coreFilePaths = [
'/src/_data/core',
'/src/_includes/core',
'/src/assets/img/core',
'/src/assets/css/core',
'/src/assets/js/core',
'/src/assets/json/core',
'/src/assets/vendor',
'/.core-eleventy.js',
'/.jam-on/core',
jamOnPath,
];
const coreFileReplacements = coreFilePaths.map((filePath) => [
`./${tmpCheckoutDir}${filePath}`,
`.${filePath}`,
]);
const repoUrls = {
odsGitLab:
'https://git.ontariogovernment.ca/service-integration/application-development-toolkit/jamstack-application-toolkit',
odsGitHub:
'https://github.com/ongov/Ontario.ca-Jamstack-Application-Toolkit',
};
const defaultRepoUrl = repoUrls.odsGitHub;
const repoUrl = options.repo ? options.repo : defaultRepoUrl;
function cleanUpTmp() {
fs.removeSync(tmpDir);
console.log(`Removed temporary checkout directory ${tmpDir}`);
fs.removeSync(tmpCheckoutDir);
console.log(`Removed temporary checkout directory ${tmpCheckoutDir}`);
}
git().clone(
repoUrl,
tmpCheckoutDir,
{ '--depth': 1, '--branch': `${tagOrBranch}` },
(error) => {
if (error) {
console.log('Error cloning specified repo');
console.log(error);
process.exit();
}
console.log(
`Checked out tag/branch ${tagOrBranch} to temporary directory ${tmpCheckoutDir}`,
);
// File diffing here
console.log(
`checking that jam-on.mjs is up to date for tag/branch ${tagOrBranch}`,
);
git().diff(
[
'--no-index',
'--numstat',
`./${jamOnPath}`,
`${tmpCheckoutDir}${jamOnPath}`,
],
(err, diff) => {
if (err) {
console.log(err);
console.log('Error running diff on jam-on.mjs file');
cleanUpTmp();
process.exit();
}
if (diff !== '') {
console.log('The jam-on.mjs CLI needs to be updated...');
fs.copySync(`${tmpCheckoutDir}${jamOnPath}`, `./${jamOnPath}`);
console.log(
'jam-on.mjs has been updated - please run the update command again',
);
cleanUpTmp();
process.exit();
}
console.log(
'jam-on.mjs is up to date, proceeding with update...',
);
ignoreFile = `${tmpCheckoutDir}${path.sep}.gitignore`;
ignoreFileContent = '*';
fs.outputFileSync(ignoreFile, ignoreFileContent);
try {
coreFileReplacements.forEach((filePathStruct) => {
console.log(
`Replacing ${filePathStruct[1]} with ${filePathStruct[0]}`,
);
fs.copySync(filePathStruct[0], filePathStruct[1]);
});
fs.removeSync(tmpDir);
console.log(`Removed temporary checkout directory ${tmpDir}`);
fs.removeSync(tmpCheckoutDir);
console.log(
`Removed temporary checkout directory ${tmpCheckoutDir}`,
);
generateToolkitFile(
'.jam-on/app/versionMetadata.json',
JSON.stringify({
tagOrBranch,
updatedOn: new Date().toISOString(),
}),
'Created version metadata file at .jam-on/app/versionMetadata.json',
);
inquirer
.prompt([
{
type: 'confirm',
default: false,
name: 'generateSitemap',
message: 'Do you want a sitemap template generated?',
},
{
type: 'input',
name: 'enRoot',
message:
"What is the English-language subfolder for deployment? (optional, defaults to 'englishRoot' in .jam-on/app/conf.json)",
},
])
.then((responses) => {
if (responses.generateSitemap) {
const enRoot = responses.enRoot
? responses.enRoot
: JSON.parse(fs.readFileSync('.jam-on/app/conf.json'))[
'englishRoot'
];
console.log(
`Creating sitemap template with front matter permalink value: ${enRoot}/sitemap.xml`,
);
const sitemapFileContent = nunjucks.render(
'sitemap.njk',
{ englishRoot: enRoot },
);
generateToolkitFile(
`src/sitemap.njk`,
sitemapFileContent,
`Wrote sitemap.njk file at src/sitemap.njk`,
);
console.warn(
`Add the line: \n'eleventyExcludeFromCollections: true' \nto a page's front-matter if you wish it to not be indexed by the sitemap`,
);
}
})
.catch((e) =>
console.error('exception while trying to update files'),
);
} catch (except) {
console.log(
'Exception when copying update files from checked out repo',
);
cleanUpTmp();
}
},
);
},
);
})
.catch((error) => {
console.log(error);
});
}
program
.name('jam-on')
.description('Developer CLI for Ontario.ca Jamstack Toolkit')
.version('0.4.0');
program
.command('new')
.description(
'put a newly cloned toolkit project into a ready state for development',
)
.option('--keepGit', 'Do not delete the local .git folder (optional)')
.action((options) => newAction(options));
program
.command('update')
.description('Update an existing Ontario.ca Jamstack project')
.argument('<tagOrBranch>', 'tag or branch to update to (required)')
.option(
'-r, --repo <repo>',
'repo URL to use, defaults to ODS GitHub (optional)',
)
.action((tagOrBranch, options) => updateAction(tagOrBranch, options));
program.parse();