modernize.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ['build:test', 'test', 'test:cov', 'test:debug', 'watch'].forEach(script => {
  43. targetData['scripts'][script] = sourceData['scripts'][script];
  44. });
  45. utils.writeJSONFile(path.join(pkgPath, name, 'package.json'), targetData);
  46. // Update tsconfigs.json - Remove skipLibCheck (added because jest and mocha types confict)
  47. const tsData = utils.readJSONFile(path.join(testSrc, 'tsconfig.json'));
  48. delete tsData['compilerOptions']['skipLibCheck'];
  49. utils.writeJSONFile(path.join(testSrc, 'tsconfig.json'), tsData);
  50. // Update the test files to use imports from `../src`
  51. glob.sync(path.join(testSrc, 'src', '**', '*.ts*')).forEach(function(filePath) {
  52. console.debug(filePath);
  53. let src = fs.readFileSync(filePath, 'utf8');
  54. src = src.split(`'@jupyterlab/${name}/src';`).join(`'../src';`);
  55. fs.writeFileSync(filePath, src, 'utf8');
  56. });
  57. // Commit changes (needed for jlpm jest-codemods)
  58. utils.run(
  59. `git add test-${name} && git commit -m "wip modernize ${name} tests"`
  60. );
  61. // Run jest-codemods to convert from chai to jest.
  62. console.debug('------------------------------------');
  63. console.debug('Select the following options');
  64. console.debug('TypeScript & TSX');
  65. console.debug('Chai : Should / Expect BDD Syntax');
  66. console.debug("Yes, and I'm not afraid of false positive transformations");
  67. console.debug('Yes, use the globals provided by Jest(recommended)');
  68. console.debug('.');
  69. console.debug('------------------------------------');
  70. utils.run('jlpm jest-codemods', { cwd: testSrc });
  71. // Move the test files to `/packages/{name}/test`
  72. utils.run(`git mv ${testSrc}/src ${pkgPath}/${name}/test`);
  73. ['tsconfig.test.json', 'babel.config.js', 'jest.config.js'].forEach(fname => {
  74. utils.run(`mv ${testSrc}/${fname} ${pkgPath}/${name}`);
  75. });
  76. // Add a vscode launch file and force it to commit.
  77. utils.run(`mkdir -p ${pkgPath}/${name}/.vscode`);
  78. utils.run(
  79. `cp ${pkgPath}/console/.vscode/launch.json ${pkgPath}/${name}/.vscode`
  80. );
  81. utils.run(`git add -f ${pkgPath}/${name}/.vscode/launch.json`);
  82. // Run integrity and build the new tests
  83. const rootDir = path.resolve('..');
  84. utils.run(`jlpm integrity && cd packages/${name} && jlpm run build:test`, {
  85. cwd: rootDir
  86. });
  87. // Remove local folder
  88. utils.run(`git rm -rf ${testSrc}`);
  89. // Commit the changes
  90. utils.run(
  91. `git add ${pkgPath}/${name} && git commit --no-verify -m "wip modernize ${name} tests`
  92. );