modernize.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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`
  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. fs.writeFileSync(filePath, src, 'utf8');
  63. });
  64. // Commit changes (needed for jlpm jest-codemods)
  65. utils.run(
  66. `git add test-${name}; git add ../packages/${name}; git commit -m "wip modernize ${name} tests"; true`
  67. );
  68. // Run jest-codemods to convert from chai to jest.
  69. console.debug('------------------------------------');
  70. console.debug('Select the following options');
  71. console.debug('TypeScript & TSX');
  72. console.debug('Chai : Should / Expect BDD Syntax');
  73. console.debug("Yes, and I'm not afraid of false positive transformations");
  74. console.debug('Yes, use the globals provided by Jest(recommended)');
  75. console.debug('.');
  76. console.debug('------------------------------------');
  77. utils.run('jlpm jest-codemods', { cwd: testSrc });
  78. // Move the test files to `/packages/{name}/test`
  79. utils.run(`git mv ${testSrc}/src ${pkgPath}/${name}/test`);
  80. ['tsconfig.test.json', 'babel.config.js', 'jest.config.js'].forEach(fname => {
  81. utils.run(`mv ${testSrc}/${fname} ${pkgPath}/${name}`);
  82. });
  83. // Add a vscode launch file and force it to commit.
  84. utils.run(`mkdir -p ${pkgPath}/${name}/.vscode`);
  85. utils.run(
  86. `cp ${pkgPath}/console/.vscode/launch.json ${pkgPath}/${name}/.vscode`
  87. );
  88. utils.run(`git add -f ${pkgPath}/${name}/.vscode/launch.json`);
  89. // Run integrity
  90. const rootDir = path.resolve('..');
  91. utils.run(`jlpm integrity`, {
  92. cwd: rootDir
  93. });
  94. // Remove local folder
  95. utils.run(`git rm -rf ${testSrc}`);
  96. // Commit the changes
  97. utils.run(
  98. `git add ${pkgPath}/${name} && git commit --no-verify -m "wip modernize ${name} tests`
  99. );