-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.js
executable file
·621 lines (520 loc) · 19.5 KB
/
build.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#!/usr/bin/env node
// IMPORTANT: The declaration order control how we will update/publish the packages
// Example: Express is dependant on BodyParser, Qs
// So we need to write: [ Qs, BodyParser, Express ]
const projects =
[
"Chalk",
"After",
"Connect",
"BodyParser",
"Methods",
"Mime",
"Qs",
"RangeParser",
"RangeParser.Extensions",
"ExpressServeStaticCore",
"ServeStatic",
"SuperAgent",
"SuperTest",
"Express"
]
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import concurrently from 'concurrently'
import { resolve, dirname, join } from "path"
import { promises as fs } from 'fs'
import fg from "fast-glob"
import { promisify } from 'util'
import execOriginal from 'child_process'
const exec = promisify(execOriginal.exec)
const spawn = promisify(execOriginal.spawn)
import parseChangelog from 'changelog-parser'
import awaitSpawn from "./scripts/await-spawn.js"
import chalkPkg from 'chalk';
const { blueBright, yellow, red, green } = chalkPkg;
import prompts from "prompts"
import { initialChangelog, initialReadme, initialGlueFsproj, initialGlueFsharpFile, initialGlueTestFsproj, initialGlueTestFsharpFile } from "./scripts/init-glue-templates.js"
import { dirname as dirnameEsm } from 'dirname-filename-esm';
const __dirname = dirnameEsm(import.meta);
const info = blueBright
const warn = yellow
const error = red
const success = green
const log = console.log
const checkIfFileExist = async function(filePath) {
try {
await fs.access(filePath)
return true;
} catch (e) {
return false;
}
}
const cleanCompiledFiles = async function() {
const entries =
await fg([
"glues/**/*.fs.js",
"tests-shared/**/*.fs.js",
]);
// Delete all the files generated by Fable
for (const entry of entries) {
await fs.unlink(entry)
}
// Delete .fable cache folders
const fableCacheFolders =
await fg([
"glues/**/.fable"
], {
onlyDirectories: true,
markDirectories: true
})
for (const fableCacheFolder of fableCacheFolders) {
await fs.rm(fableCacheFolder, { recursive: true, force: true })
}
}
const getEnvVariable = function(varName) {
const value = process.env[varName];
if (value === undefined) {
log(error(`Missing environnement variable ${varName}`))
process.exit(1)
} else {
return value;
}
}
const findRequiredSingleFile = async (pattern, projectPath) => {
const glob = await fg(pattern)
if (glob.length === 0) {
log(error(`fsproj file not found in ${projectPath}`))
process.exit(1)
}
if (glob.length > 1) {
log(error(`Several fsproj file found in ${projectPath}. It should only have one`))
process.exit(1)
}
return glob[0];
}
const findOptionalSingleFile = async (pattern, projectPath) => {
const glob = await fg(pattern)
if (glob.length === 0) {
return null;
}
if (glob.length > 1) {
log(error(`Several fsproj file found in ${projectPath}. It should only have one`))
process.exit(1)
}
return glob[0];
}
const updateTestsCountHandler = async () => {
log(warn("Command not yet ready"))
// Get the list of root test folders suffixed with a trailing '/'
// const rootTestFolders =
// await fg([
// "src/*/tests/*",
// "!tests/bin",
// "!tests/obj"
// ], {
// onlyDirectories: true,
// markDirectories: true
// })
// let bindingsTestInfo = []
// let testRunnerCount = 0
// for (const rootFolder of rootTestFolders) {
// testRunnerCount++
// const bindingNamePrefix = "/tests"
// // Remove '/tests' prefix and the trailing '/' from the rootFolder
// const bindingName = rootFolder.substr(bindingNamePrefix.length).slice(0, -1)
// log(`Executing ${bindingName} - ${testRunnerCount}/${rootTestFolders.length}`)
// const res = await exec(`npx mocha -r esm -r -r tests-shared/mocha.env.js --reporter json --recursive ${rootFolder}`);
// const jsonResult = JSON.parse(res.stdout);
// bindingsTestInfo.push({
// bindingName: bindingName,
// count: jsonResult.stats.tests
// });
// }
// const testStatusTableBody =
// bindingsTestInfo
// .map((info) => {
// return `| ${info.bindingName} | ${info.count} |`
// })
// const readmePath = path.resolve(__dirname, "README.md")
// const readmeContent = await fs.readFile(readmePath)
// const readmeLines =
// readmeContent
// .toString()
// .replace("\r\n", "\n")
// .split("\n")
// const beginTestsStatusLineIndex = readmeLines.indexOf("<!-- DON'T REMOVE - begin tests status -->");
// const endTestsStatusLineIndex = readmeLines.indexOf("<!-- DON'T REMOVE - end tests status -->");
// const newReadmeContent =
// []
// .concat(
// readmeLines.slice(0, beginTestsStatusLineIndex + 1),
// [
// "", // This create an empty line
// "| Binding | Number of tests |",
// "|---------|-----------------|"
// ],
// testStatusTableBody,
// readmeLines.slice(endTestsStatusLineIndex - 1)
// )
// .join("\n")
// await fs.writeFile(readmePath, newReadmeContent)
}
const findProjectFsproj = async (project) => {
return await findRequiredSingleFile(`glues/${project}/src/*.fsproj`, `glues/${project}/src`)
}
const findProjectChangelog = async (project) => {
const changelogPath = resolve(__dirname, "glues", project, "CHANGELOG.md")
// Check that changelog file exist
try {
await fs.access(changelogPath)
} catch (error) {
log(error(`Missing ${changelogPath} file`))
process.exit(1)
}
return changelogPath;
}
const runtestForProjectInWatchMode = async (project) => {
const testFsprojPath = await findRequiredSingleFile(`glues/${project}/tests/*.fsproj`, `glues/${project}/tests`)
const testFsprojDirectory = dirname(testFsprojPath)
const testFolderToWatch = `glues/${project}/tests`
await concurrently(
[
{
command: `nodemon --inspect --watch ${testFolderToWatch} --exec "npx mocha -r tests-shared/mocha.env.js --reporter dot --recursive ${testFolderToWatch}"`,
},
{
// There is a bug in concurrently where cwd in command options is not taken into account
// Waiting for https://github.com/kimmobrunfeldt/concurrently/pull/266 to merge
command: `cd ${testFsprojDirectory} && dotnet fable --watch`,
cwd: testFsprojDirectory
}
],
{
prefix: "none" // Disable name prefix
}
)
}
const runTestForAProject = async (project) => {
const projectFsprojPath = await findProjectFsproj(project)
const testFsprojPath = await findOptionalSingleFile(`glues/${project}/tests/*.fsproj`, `glues/${project}/tests`)
log(info("=================="))
log(info(`Begin testing ${project}`))
// If there is no tests project, we compile the glues definition to make sure everything is ok in the project
if (testFsprojPath === null) {
log(`No tests project found for ${project}, testing the bindings using 'dotnet build'`)
try {
await awaitSpawn(
"dotnet",
`build ${projectFsprojPath}`.split(" "),
{
stdio: "inherit",
shell: true
}
)
} catch (e) {
log(error(`Error while compiling ${project}. Stopping here`))
process.exit(1)
}
log(info(`Testing ${project} done`))
log(info("==================\n\n"))
return; // Stop here
}
// Compile the tests using Fable
try {
await awaitSpawn(
"dotnet",
`fable ${testFsprojPath}`.split(" "),
{
stdio: "inherit",
shell: true
}
)
// Run the tests using mocha
await awaitSpawn(
"npx",
`mocha -r tests-shared/mocha.env.js --reporter dot --recursive glues/${project}/tests`.split(" "),
{
stdio: "inherit",
shell: true,
}
)
} catch (e) {
log(error(`Error while compiling or running the tests for ${project}. Stopping here`))
process.exit(1)
}
log(info(`Testing ${project} done`))
log(info("==================\n\n"))
}
const testRunner = async (argv) => {
await cleanCompiledFiles()
if (argv.watch === true && argv.project === undefined) {
log(error("Options --watch can only be used if you specify a project"))
process.exit(1)
}
if (argv.watch) {
// Compile and test in watch mode
const project =
projects.find((p) => {
return p.toLocaleLowerCase() === argv.project.toLocaleLowerCase()
})
if (project === undefined) {
log(error(`Project '${argv.project}' not found. If you just created it, please make sure to add it to the projects list in build.js file`))
process.exit(1)
}
await runtestForProjectInWatchMode(project)
} else {
// Compile and test once then exit
if (argv.project !== undefined) {
const project =
projects.find((p) => {
return p.toLocaleLowerCase() === argv.project.toLocaleLowerCase()
})
if (project === undefined) {
log(error(`Project '${argv.project}' not found. If you just created it, please make sure to add it to the projects list in build.js file`))
process.exit(1)
}
await runTestForAProject(project)
} else {
for (const project of projects) {
await runTestForAProject(project)
}
}
}
}
const publishHandler = async () => {
// Check if all the required env variables are defined
const NUGET_KEY = getEnvVariable("NUGET_KEY")
// 1. Remove Fable compiled files
await cleanCompiledFiles()
for (const project of projects) {
await runTestForAProject(project)
const projectFsproj = await findProjectFsproj(project)
const changelogPath = await findProjectChangelog(project)
const fsprojContent = (await fs.readFile(projectFsproj)).toString()
// Normalize the new lines otherwise parseChangelog isn't able to parse the file correctly
const changelogContent = (await fs.readFile(changelogPath)).toString().replace("\r\n", "\n")
const changelog = await parseChangelog({ text: changelogContent })
// Check if the changelog has at least 2 versions in it
// Unreleased & X.Y.Z
if (changelog.versions.length < 2) {
log(error(`No version to publish for ${project}`))
process.exit(1)
}
const unreleased = changelog.versions[0];
// Check malformed changelog
if (unreleased.title !== "Unreleased") {
log(error(`Malformed CHANGELOG.md file in ${project}`))
log(error("The changelog should first version should be 'Unreleased'"))
process.exit(1)
}
// Access via index is ok we checked the length before
const newVersion = changelog.versions[1].version;
if (newVersion.version === null) {
log(error(`Malformed CHANGELOG.md file in ${project}`))
log(error("Please verify the last version format, it should be SEMVER compliant"))
process.exit(1)
}
const fsprojVersionRegex = /<Version>(.*)<\/Version>/gmi
const m = fsprojVersionRegex.exec(fsprojContent)
if (m === null) {
log(error(`Missing <Version>..</Version> tag in ${projectFsproj}`))
process.exit(1)
}
const lastPublishedVersion = m[1];
if (lastPublishedVersion === newVersion) {
log(`Version ${lastPublishedVersion} of ${project}, has already been published. Skipping this project`)
continue;
}
log(`New version detected for ${project}, starting publish process for it`)
const newFsprojContent = fsprojContent.replace(fsprojVersionRegex, `<Version>${newVersion}</Version>`)
// Start a try-catch here, because we modfied the file on the disk
// This allows to revert the changes made to the file is something goes wrong
try {
// Update fsproj file on the disk
await fs.writeFile(projectFsproj, newFsprojContent)
await awaitSpawn(
"dotnet",
`pack -c Release ${projectFsproj}`.split(' '),
{
stdio: "inherit",
shell: true
}
)
const nugetPackagePath = await findRequiredSingleFile(`glues/${project}/src/bin/Release/*${newVersion}.nupkg`)
await awaitSpawn(
"dotnet",
`nuget push -s nuget.org -k ${NUGET_KEY} ${nugetPackagePath}`.split(' '),
{
stdio: "inherit",
shell: true
}
)
log(success(`Project ${project} has been published`))
} catch (e) {
log(error(`Something went wrong while publish ${project}`))
log("Reverting changes made to the files")
await fs.writeFile(projectFsproj, fsprojContent)
log("Revert done")
process.exit(1)
}
}
}
const initNewGlue = async (argv) => {
const requiredTextPrompt = (input) => {
if (input !== undefined && input !== null && input !== "") {
return true
} else {
return "This field is required"
}
}
const response = await prompts([
{
type: "text",
name: "glueName",
message: "What is the glue name?",
validate: requiredTextPrompt
},
{
type: "text",
name: "npmPackageName",
message: "What is the Npm package name?",
validate: requiredTextPrompt
},
{
type: "text",
name: "npmUrl",
message: "What is the Npm package url?",
validate: requiredTextPrompt
},
{
type: "text",
name: "authors",
message: "What is the authors value that should be added in the NuGet package?",
validate: requiredTextPrompt
}
])
const glueRootPath = resolve(__dirname, "glues", response.glueName)
// Src folder
const glueSrcPath = join(glueRootPath, "src")
const glueChangelog = join(glueRootPath, "CHANGELOG.md")
const glueReadme = join(glueRootPath, "README.md")
const glueFsproj = join(glueSrcPath, `Glutinum.${response.glueName}.fsproj`)
const glueFsarpFile = join(glueSrcPath, `Glutinum.${response.glueName}.fs`)
// Tests folder
const glueTestsPath = join(glueRootPath, "tests")
const glueTestsFsproj = join(glueTestsPath, `Tests.${response.glueName}.fsproj`)
const glueTestsFsarpFile = join(glueTestsPath, `Tests.${response.glueName}.fs`)
const glueAlreadyExist = await checkIfFileExist(glueRootPath)
if (glueAlreadyExist) {
log(error(`A glue already exist with the same name`))
process.exit(1)
}
// Glue doens't exist we can create it
try {
await fs.mkdir(glueRootPath)
// Init root files like CHANGELOG, README
await fs.writeFile(glueChangelog, initialChangelog())
await fs.writeFile(glueReadme, initialReadme(response.glueName, response.npmUrl, response.npmPackageName))
// Init the src folder
await fs.mkdir(glueSrcPath)
await fs.writeFile(glueFsproj, initialGlueFsproj(response.glueName, response.authors, response.npmUrl))
await fs.writeFile(glueFsarpFile, initialGlueFsharpFile(response.glueName, response.npmPackageName))
// Add latest version of the Fable.Core to the fsproj
await awaitSpawn(
"dotnet",
`add ${glueFsproj} package Fable.Core`.split(" "),
{
stdio: "inherit",
shell: true
}
)
// Init tests folder
await fs.mkdir(glueTestsPath)
await fs.writeFile(glueTestsFsproj, initialGlueTestFsproj(response.glueName))
await fs.writeFile(glueTestsFsarpFile, initialGlueTestFsharpFile(response.glueName))
// Add latest version of the Fable.Core to the fsproj
await awaitSpawn(
"dotnet",
`add ${glueTestsFsproj} package Fable.Core`.split(" "),
{
stdio: "inherit",
shell: true
}
)
log(success(`Glue ${response.glueName} has been created`))
log(success(`You need to add ${response.glueName} to the projects list in the 'build.js' file (at the top of the file)`))
} catch (e) {
log(error(`An error occured during the glue creation. Please check the console for error message and delete the folder 'glues/${response.glueName}' before retrying`))
}
}
yargs(hideBin(process.argv))
.completion()
.strict()
.help()
.alias("help", "h")
.command(
"update-tests-count",
"Execute the different tests and update tests count section of the README file",
() => { },
updateTestsCountHandler
)
.command(
"clean",
"Delete all the compiled or cached files from dotnet, Fable.",
() => { },
async () => {
await cleanCompiledFiles()
}
)
.command(
"publish",
`1. Clean files
2. For each package make a fresh compilation and run tests
3. Update the version in the fsproj using the changelog as reference
4. Generate the packages
5. Publish new packages on NuGet
Note: If an error occured, after updating the version in the fsproj the process will try to revert the changes on the current fsproj file.
`,
() => { },
publishHandler
)
.command(
"test",
`Run test against project(s)
By default, it will run test against all the projects.
You can specify a project to run the test only for this one.
`,
(argv) => {
argv
.option(
"project",
{
description: `Name of the project you want to run the test for`,
alias: "p",
type: "string"
}
)
.option(
"watch",
{
description:
`Start Fable and Mocha in watch mode
This option can only be used if you specify a project
`,
alias: "w",
type: "boolean",
default: false
}
)
},
testRunner
)
.command(
"init",
"Initialize a new glue package, you will be guided during the initialization process",
() => { },
initNewGlue
)
.version(false)
.argv