package.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. import * as child_process from 'child_process';
  2. import * as crypto from 'crypto';
  3. import * as fs from 'fs/promises';
  4. import * as os from 'os';
  5. import * as path from 'path';
  6. import * as url from 'url';
  7. const args = process.argv.slice(2);
  8. const read = (match) => {
  9. if (args.length > 0 || (!match || args[0] === match)) {
  10. return args.shift();
  11. }
  12. return null;
  13. };
  14. let configuration = null;
  15. const dirname = (...args) => {
  16. const file = url.fileURLToPath(import.meta.url);
  17. const dir = path.dirname(file);
  18. return path.join(dir, ...args);
  19. };
  20. const load = async () => {
  21. const file = dirname('package.json');
  22. const content = await fs.readFile(file, 'utf-8');
  23. configuration = JSON.parse(content);
  24. };
  25. const clearLine = () => {
  26. if (process.stdout.clearLine) {
  27. process.stdout.clearLine();
  28. }
  29. };
  30. const write = (message) => {
  31. if (process.stdout.write) {
  32. process.stdout.write(message);
  33. }
  34. };
  35. const writeLine = (message) => {
  36. write(message + os.EOL);
  37. };
  38. const access = async (path) => {
  39. try {
  40. await fs.access(path);
  41. return true;
  42. } catch {
  43. return false;
  44. }
  45. };
  46. const rm = async (...args) => {
  47. const dir = dirname(...args);
  48. const exists = await access(dir);
  49. if (exists) {
  50. const paths = path.join(...args);
  51. writeLine(`rm ${paths}`);
  52. const options = { recursive: true, force: true };
  53. await fs.rm(dir, options);
  54. }
  55. };
  56. const mkdir = async (...args) => {
  57. const dir = dirname(...args);
  58. const exists = await access(dir);
  59. if (!exists) {
  60. const paths = path.join(...args);
  61. writeLine(`mkdir ${paths}`);
  62. const options = { recursive: true };
  63. await fs.mkdir(dir, options);
  64. }
  65. return dir;
  66. };
  67. const copy = async (source, target, filter) => {
  68. let files = await fs.readdir(source);
  69. files = filter ? files.filter((file) => filter(file)) : files;
  70. const promises = files.map((file) => fs.copyFile(path.join(source, file), path.join(target, file)));
  71. await Promise.all(promises);
  72. };
  73. const unlink = async (dir, filter) => {
  74. let files = await fs.readdir(dir);
  75. files = filter ? files.filter((file) => filter(file)) : files;
  76. const promises = files.map((file) => fs.unlink(path.join(dir, file)));
  77. await Promise.all(promises);
  78. };
  79. const exec = async (command, encoding, cwd) => {
  80. cwd = cwd || dirname();
  81. if (encoding) {
  82. return child_process.execSync(command, { cwd, encoding });
  83. }
  84. child_process.execSync(command, { cwd, stdio: [0,1,2] });
  85. return '';
  86. /*
  87. return new Promise((resolve, reject) => {
  88. const child = child_process.exec(command, { cwd: dirname() }, (error, stdout, stderr) => {
  89. if (error) {
  90. stderr = '\n' + stderr ;
  91. if (error.message && error.message.endsWith(stderr)) {
  92. error.message = error.message.slice(0, -stderr.length);
  93. }
  94. reject(error);
  95. } else {
  96. resolve(stdout);
  97. }
  98. });
  99. child.stdout.pipe(process.stdout);
  100. child.stderr.pipe(process.stderr);
  101. });
  102. */
  103. };
  104. const sleep = (delay) => {
  105. return new Promise((resolve) => {
  106. setTimeout(resolve, delay);
  107. });
  108. };
  109. const request = async (url, init, status) => {
  110. const response = await fetch(url, init);
  111. if (status !== false && !response.ok) {
  112. throw new Error(response.status.toString());
  113. }
  114. if (response.body) {
  115. const reader = response.body.getReader();
  116. let position = 0;
  117. const stream = new ReadableStream({
  118. start(controller) {
  119. const read = async () => {
  120. try {
  121. const result = await reader.read();
  122. if (result.done) {
  123. clearLine();
  124. controller.close();
  125. } else {
  126. position += result.value.length;
  127. write(` ${position} bytes\r`);
  128. controller.enqueue(result.value);
  129. read();
  130. }
  131. } catch (error) {
  132. controller.error(error);
  133. }
  134. };
  135. read();
  136. }
  137. });
  138. return new Response(stream, {
  139. status: response.status,
  140. statusText: response.statusText,
  141. headers: response.headers
  142. });
  143. }
  144. return response;
  145. };
  146. const download = async (url) => {
  147. writeLine(`download ${url}`);
  148. const response = await request(url);
  149. return response.arrayBuffer().then((buffer) => new Uint8Array(buffer));
  150. };
  151. const hash = async (url, algorithm) => {
  152. const data = await download(url);
  153. const hash = crypto.createHash(algorithm);
  154. hash.update(data);
  155. return hash.digest('hex');
  156. };
  157. const fork = async (organization, repository) => {
  158. const headers = {
  159. Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
  160. };
  161. writeLine(`github delete ${repository}`);
  162. await request(`https://api.github.com/repos/${process.env.GITHUB_USER}/${repository}`, {
  163. method: 'DELETE',
  164. headers
  165. }, false);
  166. await sleep(4000);
  167. writeLine(`github fork ${repository}`);
  168. await request(`https://api.github.com/repos/${organization}/${repository}/forks`, {
  169. method: 'POST',
  170. headers,
  171. body: ''
  172. });
  173. await sleep(4000);
  174. await rm('dist', repository);
  175. writeLine(`github clone ${repository}`);
  176. await exec(`git clone --depth=2 https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_USER}/${repository}.git dist/${repository}`);
  177. };
  178. const pullrequest = async (organization, repository, body) => {
  179. writeLine(`github push ${repository}`);
  180. await exec(`git -C dist/${repository} push`);
  181. writeLine('github pullrequest homebrew-cask');
  182. const headers = {
  183. Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
  184. };
  185. await request(`https://api.github.com/repos/${organization}/${repository}/pulls`, {
  186. method: 'POST',
  187. headers,
  188. body: JSON.stringify(body)
  189. });
  190. };
  191. const clean = async () => {
  192. await rm('dist');
  193. await rm('node_modules');
  194. await rm('package-lock.json');
  195. await rm('yarn.lock');
  196. };
  197. const install = async () => {
  198. const node_modules = dirname('node_modules');
  199. let exists = await access(node_modules);
  200. if (exists) {
  201. const dependencies = { ...configuration.dependencies, ...configuration.devDependencies };
  202. const matches = await Promise.all(Object.entries(dependencies).map(async ([name, version]) => {
  203. const file = path.join('node_modules', name, 'package.json');
  204. const exists = await access(file);
  205. if (exists) {
  206. const content = await fs.readFile(file, 'utf8');
  207. const obj = JSON.parse(content);
  208. return obj.version === version;
  209. }
  210. return false;
  211. }));
  212. exists = matches.every((match) => match);
  213. if (!exists) {
  214. await clean();
  215. }
  216. }
  217. exists = await access(node_modules);
  218. if (!exists) {
  219. await exec('npm install');
  220. }
  221. };
  222. const start = async () => {
  223. await install();
  224. await exec('npx electron .');
  225. };
  226. const purge = async () => {
  227. await clean();
  228. await rm('third_party', 'bin');
  229. await rm('third_party', 'env');
  230. await rm('third_party', 'source');
  231. };
  232. const build = async (target) => {
  233. switch (target || read()) {
  234. case 'web': {
  235. writeLine('build web');
  236. await rm('dist', 'web');
  237. await mkdir('dist', 'web');
  238. writeLine('cp source/dir dist/dir');
  239. const source_dir = dirname('source');
  240. const dist_dir = dirname('dist', 'web');
  241. const extensions = new Set(['html', 'css', 'js', 'json', 'ico', 'png']);
  242. await copy(source_dir, dist_dir, (file) => extensions.has(file.split('.').pop()));
  243. await rm('dist', 'web', 'app.js');
  244. await rm('dist', 'web', 'electron.js');
  245. const contentFile = dirname('dist', 'web', 'index.html');
  246. let content = await fs.readFile(contentFile, 'utf-8');
  247. content = content.replace(/(<meta\s*name="version"\s*content=")(.*)(">)/m, (match, p1, p2, p3) => {
  248. return p1 + configuration.version + p3;
  249. });
  250. content = content.replace(/(<meta\s*name="date"\s*content=")(.*)(">)/m, (match, p1, p2, p3) => {
  251. return p1 + configuration.date + p3;
  252. });
  253. await fs.writeFile(contentFile, content, 'utf-8');
  254. break;
  255. }
  256. case 'electron': {
  257. writeLine('build electron');
  258. await install();
  259. await exec('npx electron-builder install-app-deps');
  260. await exec('npx electron-builder --mac --universal --publish never -c.mac.identity=null');
  261. await exec('npx electron-builder --win --x64 --arm64 --publish never');
  262. await exec('npx electron-builder --linux appimage --x64 --publish never');
  263. await exec('npx electron-builder --linux snap --x64 --publish never');
  264. break;
  265. }
  266. case 'python': {
  267. writeLine('build python');
  268. await exec('python package.py build version');
  269. await exec('python -m pip install --user build wheel --quiet');
  270. await exec('python -m build --no-isolation --wheel --outdir dist/pypi dist/pypi');
  271. if (read('install')) {
  272. await exec('python -m pip install --force-reinstall dist/pypi/*.whl');
  273. }
  274. break;
  275. }
  276. default: {
  277. writeLine('build');
  278. await rm('dist');
  279. await install();
  280. await build('web');
  281. await build('electron');
  282. await build('python');
  283. break;
  284. }
  285. }
  286. };
  287. const publish = async (target) => {
  288. const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
  289. const GITHUB_USER = process.env.GITHUB_USER;
  290. switch (target || read()) {
  291. case 'web': {
  292. writeLine('publish web');
  293. await build('web');
  294. await rm('dist', 'gh-pages');
  295. const url = `https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_USER}/netron.git`;
  296. await exec(`git clone --depth=1 ${url} --branch gh-pages ./dist/gh-pages 2>&1 > /dev/null`);
  297. writeLine('cp dist/web dist/gh-pages');
  298. const source_dir = dirname('dist', 'web');
  299. const target_dir = dirname('dist', 'gh-pages');
  300. await unlink(target_dir, (file) => file !== '.git');
  301. await copy(source_dir, target_dir);
  302. await exec('git -C dist/gh-pages add --all');
  303. await exec('git -C dist/gh-pages commit --amend --no-edit');
  304. await exec('git -C dist/gh-pages push --force origin gh-pages');
  305. break;
  306. }
  307. case 'electron': {
  308. writeLine('publish electron');
  309. await install();
  310. await exec('npx electron-builder install-app-deps');
  311. await exec('npx electron-builder --mac --universal --publish always');
  312. await exec('npx electron-builder --win --x64 --arm64 --publish always');
  313. await exec('npx electron-builder --linux appimage --x64 --publish always');
  314. await exec('npx electron-builder --linux snap --x64 --publish always');
  315. break;
  316. }
  317. case 'python': {
  318. writeLine('publish python');
  319. await build('python');
  320. await exec('python -m pip install --user twine');
  321. await exec('python -m twine upload --non-interactive --skip-existing --verbose dist/pypi/*.whl');
  322. break;
  323. }
  324. case 'cask': {
  325. writeLine('publish cask');
  326. await fork('Homebrew', 'homebrew-cask');
  327. const repository = `https://github.com/${configuration.repository}`;
  328. const url = `${repository}/releases/download/v#{version}/${configuration.productName}-#{version}-mac.zip`;
  329. const sha256 = await hash(url.replace(/#{version}/g, configuration.version), 'sha256');
  330. writeLine('update manifest');
  331. const dir = await mkdir('dist', 'homebrew-cask', 'Casks', 'n');
  332. const file = path.join(dir, 'netron.rb');
  333. await fs.writeFile(file, [
  334. `cask "${configuration.name}" do`,
  335. ` version "${configuration.version}"`,
  336. ` sha256 "${sha256.toLowerCase()}"`,
  337. '',
  338. ` url "${url}"`,
  339. ` name "${configuration.productName}"`,
  340. ` desc "${configuration.description.replace('Visualizer', 'Visualiser')}"`,
  341. ` homepage "${repository}"`,
  342. '',
  343. ' auto_updates true',
  344. '',
  345. ` app "${configuration.productName}.app"`,
  346. '',
  347. ' zap trash: [',
  348. ` "~/Library/Application Support/${configuration.productName}",`,
  349. ` "~/Library/Preferences/${configuration.build.appId}.plist",`,
  350. ` "~/Library/Saved Application State/${configuration.build.appId}.savedState",`,
  351. ' ]',
  352. 'end',
  353. ''
  354. ].join('\n'));
  355. writeLine('git push homebrew-cask');
  356. await exec('git -C dist/homebrew-cask add --all');
  357. await exec(`git -C dist/homebrew-cask commit -m "${configuration.name} ${configuration.version}"`);
  358. await pullrequest('Homebrew', 'homebrew-cask', {
  359. title: `${configuration.name} ${configuration.version}`,
  360. body: 'Update version and sha256',
  361. head: `${process.env.GITHUB_USER}:master`,
  362. base: 'master'
  363. });
  364. await rm('dist', 'homebrew-cask');
  365. break;
  366. }
  367. case 'winget': {
  368. writeLine('publish winget');
  369. await fork('microsoft', 'winget-pkgs');
  370. const name = configuration.name;
  371. const version = configuration.version;
  372. const product = configuration.productName;
  373. const publisher = configuration.author.name;
  374. const identifier = `${publisher.replace(' ', '')}.${product}`;
  375. const copyright = `Copyright (c) ${publisher}`;
  376. const repository = `https://github.com/${configuration.repository}`;
  377. const url = `${repository}/releases/download/v${version}/${product}-Setup-${version}.exe`;
  378. const extensions = configuration.build.fileAssociations.map((entry) => `- ${entry.ext}`).sort().join('\n');
  379. writeLine(`download ${url}`);
  380. const sha256 = await hash(url, 'sha256');
  381. const paths = ['dist', 'winget-pkgs', 'manifests', publisher[0].toLowerCase(), publisher.replace(' ', ''), product, version];
  382. await mkdir(...paths);
  383. writeLine('update manifest');
  384. const manifestFile = dirname(...paths, identifier);
  385. await fs.writeFile(`${manifestFile}.yaml`, [
  386. '# yaml-language-server: $schema=https://aka.ms/winget-manifest.version.1.6.0.schema.json',
  387. `PackageIdentifier: ${identifier}`,
  388. `PackageVersion: ${version}`,
  389. 'DefaultLocale: en-US',
  390. 'ManifestType: version',
  391. 'ManifestVersion: 1.6.0',
  392. ''
  393. ].join('\n'));
  394. await fs.writeFile(`${manifestFile}.installer.yaml`, [
  395. '# yaml-language-server: $schema=https://aka.ms/winget-manifest.installer.1.6.0.schema.json',
  396. `PackageIdentifier: ${identifier}`,
  397. `PackageVersion: ${version}`,
  398. 'Platform:',
  399. '- Windows.Desktop',
  400. 'InstallModes:',
  401. '- silent',
  402. '- silentWithProgress',
  403. 'Installers:',
  404. '- Architecture: x86',
  405. ' Scope: user',
  406. ' InstallerType: nullsoft',
  407. ` InstallerUrl: ${url}`,
  408. ` InstallerSha256: ${sha256.toUpperCase()}`,
  409. ' InstallerLocale: en-US',
  410. ' InstallerSwitches:',
  411. ' Custom: /NORESTART',
  412. ' UpgradeBehavior: install',
  413. '- Architecture: arm64',
  414. ' Scope: user',
  415. ' InstallerType: nullsoft',
  416. ` InstallerUrl: ${url}`,
  417. ` InstallerSha256: ${sha256.toUpperCase()}`,
  418. ' InstallerLocale: en-US',
  419. ' InstallerSwitches:',
  420. ' Custom: /NORESTART',
  421. ' UpgradeBehavior: install',
  422. 'FileExtensions:',
  423. extensions,
  424. 'ManifestType: installer',
  425. 'ManifestVersion: 1.6.0',
  426. ''
  427. ].join('\n'));
  428. await fs.writeFile(`${manifestFile}.locale.en-US.yaml`, [
  429. '# yaml-language-server: $schema=https://aka.ms/winget-manifest.defaultLocale.1.6.0.schema.json',
  430. `PackageIdentifier: ${identifier}`,
  431. `PackageVersion: ${version}`,
  432. `PackageName: ${product}`,
  433. 'PackageLocale: en-US',
  434. `PackageUrl: ${repository}`,
  435. `Publisher: ${publisher}`,
  436. `PublisherUrl: ${repository}`,
  437. `PublisherSupportUrl: ${repository}/issues`,
  438. `Author: ${publisher}`,
  439. `License: ${configuration.license}`,
  440. `Copyright: ${copyright}`,
  441. `CopyrightUrl: ${repository}/blob/main/LICENSE`,
  442. `ShortDescription: ${configuration.description}`,
  443. `Description: ${configuration.description}`,
  444. `Moniker: ${name}`,
  445. 'Tags:',
  446. '- machine-learning',
  447. '- deep-learning',
  448. '- neural-network',
  449. 'ManifestType: defaultLocale',
  450. 'ManifestVersion: 1.6.0',
  451. ''
  452. ].join('\n'));
  453. writeLine('git push winget-pkgs');
  454. await exec('git -C dist/winget-pkgs add --all');
  455. await exec(`git -C dist/winget-pkgs commit -m "Update ${configuration.name} to ${configuration.version}"`);
  456. await pullrequest('microsoft', 'winget-pkgs', {
  457. title: `Update ${configuration.productName} to ${configuration.version}`,
  458. body: '',
  459. head: `${process.env.GITHUB_USER}:master`,
  460. base: 'master'
  461. });
  462. await rm('dist', 'winget-pkgs');
  463. break;
  464. }
  465. default: {
  466. writeLine('publish');
  467. await rm('dist');
  468. await install();
  469. await publish('web');
  470. await publish('electron');
  471. await publish('python');
  472. await publish('cask');
  473. await publish('winget');
  474. break;
  475. }
  476. }
  477. };
  478. const lint = async () => {
  479. await install();
  480. writeLine('eslint');
  481. await exec('npx eslint --config publish/eslint.config.js *.*js source/*.*js test/*.*js publish/*.*js tools/*.js');
  482. writeLine('pylint');
  483. await exec('python -m pip install --upgrade --quiet pylint');
  484. await exec('python -m pylint -sn --recursive=y source test publish tools *.py');
  485. };
  486. const validate = async() => {
  487. await lint();
  488. writeLine('test');
  489. await exec('node test/models.js tag:validation');
  490. };
  491. const update = async () => {
  492. const targets = process.argv.length > 3 ? process.argv.slice(3) : [
  493. 'armnn',
  494. 'bigdl',
  495. 'caffe',
  496. 'circle',
  497. 'cntk',
  498. 'coreml',
  499. 'dlc',
  500. 'dnn',
  501. 'gguf',
  502. 'keras',
  503. 'mnn',
  504. 'mslite',
  505. 'megengine',
  506. 'nnabla',
  507. 'onnx',
  508. 'om',
  509. 'paddle',
  510. 'pytorch',
  511. 'rknn',
  512. 'sentencepiece',
  513. 'sklearn',
  514. 'tf',
  515. 'uff',
  516. 'xmodel'
  517. ];
  518. for (const target of targets) {
  519. /* eslint-disable no-await-in-loop */
  520. await exec(`tools/${target} sync install schema metadata`);
  521. /* eslint-enable no-await-in-loop */
  522. }
  523. };
  524. const pull = async () => {
  525. await exec('git fetch --prune origin "refs/tags/*:refs/tags/*"');
  526. const before = await exec('git rev-parse HEAD', 'utf-8');
  527. try {
  528. await exec('git pull --prune --rebase');
  529. } catch (error) {
  530. writeLine(error.message);
  531. }
  532. const after = await exec('git rev-parse HEAD', 'utf-8');
  533. if (before.trim() !== after.trim()) {
  534. const output = await exec(`git diff --name-only ${before.trim()} ${after.trim()}`, 'utf-8');
  535. const files = new Set(output.split('\n'));
  536. if (files.has('package.json')) {
  537. await clean();
  538. await install();
  539. }
  540. }
  541. };
  542. const coverage = async () => {
  543. await rm('dist', 'nyc');
  544. await mkdir('dist', 'nyc');
  545. await exec('cp package.json dist/nyc');
  546. await exec('cp -R source dist/nyc');
  547. await exec('nyc instrument --compact false source dist/nyc/source');
  548. await exec('nyc --instrument npx electron ./dist/nyc');
  549. };
  550. const forge = async() => {
  551. const command = read();
  552. switch (command) {
  553. case 'install': {
  554. await exec('npm install @electron-forge/[email protected]');
  555. await exec('npm install @electron-forge/[email protected]');
  556. await exec('npm install @electron-forge/[email protected]');
  557. await exec('npm install @electron-forge/[email protected]');
  558. await exec('npm install @electron-forge/[email protected]');
  559. break;
  560. }
  561. case 'update': {
  562. const cwd = path.join(dirname(), '..', 'forge');
  563. const node_modules = path.join(cwd, 'node_modules');
  564. const links = path.join(cwd, '.links');
  565. const exists = await access(node_modules);
  566. if (!exists) {
  567. await exec('yarn', null, cwd);
  568. }
  569. await exec('yarn build', null, cwd);
  570. await exec('yarn link:prepare', null, cwd);
  571. await exec(`yarn link @electron-forge/core --link-folder=${links}`);
  572. break;
  573. }
  574. case 'build': {
  575. await exec('npx electron-forge make');
  576. break;
  577. }
  578. default: {
  579. throw new Error(`Unsupported forge command ${command}.`);
  580. }
  581. }
  582. };
  583. const analyze = async () => {
  584. const exists = await access('third_party/tools/codeql');
  585. if (!exists) {
  586. await exec('git clone --depth=1 https://github.com/github/codeql.git third_party/tools/codeql');
  587. }
  588. await rm('dist', 'codeql');
  589. await mkdir('dist', 'codeql', 'netron');
  590. await exec('cp -r publish source test tools dist/codeql/netron/');
  591. await exec('codeql database create dist/codeql/database --source-root dist/codeql/netron --language=javascript --threads=3');
  592. await exec('codeql database analyze dist/codeql/database ./third_party/tools/codeql/javascript/ql/src/codeql-suites/javascript-security-and-quality.qls --format=csv --output=dist/codeql/results.csv --threads=3');
  593. await exec('cat dist/codeql/results.csv');
  594. };
  595. const version = async () => {
  596. await pull();
  597. const file = dirname('package.json');
  598. let content = await fs.readFile(file, 'utf-8');
  599. content = content.replace(/(\s*"version":\s")(\d\.\d\.\d)(",)/m, (match, p1, p2, p3) => {
  600. const version = Array.from((parseInt(p2.split('.').join(''), 10) + 1).toString()).join('.');
  601. return p1 + version + p3;
  602. });
  603. content = content.replace(/(\s*"date":\s")(.*)(",)/m, (match, p1, p2, p3) => {
  604. const date = new Date().toISOString().split('.').shift().split('T').join(' ');
  605. return p1 + date + p3;
  606. });
  607. await fs.writeFile(file, content, 'utf-8');
  608. await load();
  609. await exec('git add package.json');
  610. await exec(`git commit -m "Update to ${configuration.version}"`);
  611. await exec(`git tag v${configuration.version}`);
  612. await exec('git push');
  613. await exec('git push --tags');
  614. };
  615. const next = async () => {
  616. try {
  617. const task = read();
  618. switch (task) {
  619. case 'start': await start(); break;
  620. case 'clean': await clean(); break;
  621. case 'purge': await purge(); break;
  622. case 'install': await install(); break;
  623. case 'build': await build(); break;
  624. case 'publish': await publish(); break;
  625. case 'version': await version(); break;
  626. case 'lint': await lint(); break;
  627. case 'validate': await validate(); break;
  628. case 'update': await update(); break;
  629. case 'pull': await pull(); break;
  630. case 'analyze': await analyze(); break;
  631. case 'coverage': await coverage(); break;
  632. case 'forge': await forge(); break;
  633. default: throw new Error(`Unsupported task '${task}'.`);
  634. }
  635. } catch (err) {
  636. if (process.stdout.write) {
  637. process.stdout.write(err.message + os.EOL);
  638. }
  639. process.exit(1);
  640. }
  641. };
  642. load().then(() => next());