modernize.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* eslint-disable no-console */
  2. const fs = require('fs-extra');
  3. const glob = require('glob');
  4. const path = require('path');
  5. const utils = require('@jupyterlab/buildutils');
  6. let target = process.argv[2];
  7. if (!target) {
  8. console.error('Specify a target dir');
  9. process.exit(1);
  10. }
  11. if (target.indexOf('test-') !== 0) {
  12. target = 'test-' + target;
  13. }
  14. // Make sure folder exists
  15. let testSrc = path.join(__dirname, target);
  16. console.debug(testSrc); // eslint-disable-line
  17. if (!fs.existsSync(testSrc)) {
  18. console.debug('bailing'); // eslint-disable-line
  19. process.exit(1);
  20. }
  21. const pkgPath = path.resolve(path.join(__dirname, '../packages'));
  22. const name = target.replace('test-', '');
  23. // Convert to jest if needed
  24. if (fs.existsSync(path.join(testSrc, 'karma.conf.js'))) {
  25. utils.run(`node convert-to-jest.js ${name}`);
  26. }
  27. // Copy files from console
  28. ['tsconfig.test.json', 'babel.config.js', 'jest.config.js'].forEach(fname => {
  29. const srcPath = path.join(pkgPath, 'console', fname);
  30. fs.copySync(srcPath, path.join(testSrc, fname));
  31. });
  32. // Update target package.json
  33. const sourceData = utils.readJSONFile(
  34. path.join(pkgPath, 'console', 'package.json')
  35. );
  36. const targetData = utils.readJSONFile(path.join(pkgPath, name, 'package.json'));
  37. // Add dev dependencies
  38. ['@jupyterlab/testutils', '@types/jest', 'jest', 'ts-jest'].forEach(dep => {
  39. targetData['devDependencies'][dep] = sourceData['devDependencies'][dep];
  40. });
  41. // Update scripts
  42. [
  43. 'build:test',
  44. 'test',
  45. 'test:cov',
  46. 'test:debug',
  47. 'test:debug:watch',
  48. 'watch'
  49. ].forEach(script => {
  50. targetData['scripts'][script] = sourceData['scripts'][script];
  51. });
  52. utils.writeJSONFile(path.join(pkgPath, name, 'package.json'), targetData);
  53. // Update tsconfigs.json - Remove skipLibCheck (added because jest and mocha types confict)
  54. const tsData = utils.readJSONFile(path.join(testSrc, 'tsconfig.json'));
  55. delete tsData['compilerOptions']['skipLibCheck'];
  56. utils.writeJSONFile(path.join(testSrc, 'tsconfig.json'), tsData);
  57. // Update the test files to use imports from `../src` and import "jest"
  58. glob.sync(path.join(testSrc, 'src', '**', '*.ts*')).forEach(function(filePath) {
  59. console.debug(filePath);
  60. let src = fs.readFileSync(filePath, 'utf8');
  61. src = src.split(`'@jupyterlab/${name}/src';`).join(`'../src';`);
  62. let lines = src.split('\n');
  63. let i = 0;
  64. while (!lines[i++].startsWith('//')) {
  65. // no-op
  66. }
  67. lines.splice(i, 0, ["\nimport 'jest';"]);
  68. fs.writeFileSync(filePath, lines.join('\n'), 'utf8');
  69. });
  70. // Run jest-codemods to convert from chai to jest.
  71. console.debug('------------------------------------');
  72. console.debug('Select the following options');
  73. console.debug('TypeScript & TSX');
  74. console.debug('Chai : Should / Expect BDD Syntax');
  75. console.debug("Yes, and I'm not afraid of false positive transformations");
  76. console.debug('Yes, use the globals provided by Jest(recommended)');
  77. console.debug('.');
  78. console.debug('------------------------------------');
  79. utils.run('jlpm jest-codemods --force', { cwd: testSrc });
  80. // Move the test config files to `/packages/{name}`
  81. utils.run(`git mv ${testSrc}/src ${pkgPath}/${name}/test`);
  82. ['tsconfig.test.json', 'babel.config.js', 'jest.config.js'].forEach(fname => {
  83. utils.run(`mv ${testSrc}/${fname} ${pkgPath}/${name}`);
  84. });
  85. utils.run(`git add ${pkgPath}/${name}`);
  86. // Add a vscode launch file and force it to commit.
  87. utils.run(`mkdir -p ${pkgPath}/${name}/.vscode`);
  88. utils.run(
  89. `cp ${pkgPath}/console/.vscode/launch.json ${pkgPath}/${name}/.vscode`
  90. );
  91. utils.run(`git add -f ${pkgPath}/${name}/.vscode/launch.json`);
  92. // Remove local folder
  93. utils.run(`git rm -rf ${testSrc} && rm -rf ${testSrc}`);
  94. // Run integrity
  95. const rootDir = path.resolve('..');
  96. utils.run(`jlpm integrity`, {
  97. cwd: rootDir
  98. });