build.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/env node
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. /* eslint-disable no-console */
  21. /**
  22. * Build packages/plugins filtered by globs
  23. */
  24. process.env.PATH = `./node_modules/.bin:${process.env.PATH}`;
  25. const { spawnSync } = require('child_process');
  26. const fastGlob = require('fast-glob');
  27. const { argv } = require('yargs');
  28. const { _: globs } = argv;
  29. const glob = globs.length > 1 ? `{${globs.join(',')}}` : globs[0] || '*';
  30. const BABEL_CONFIG = '--config-file=../../babel.config.js';
  31. // packages that do not need tsc
  32. const META_PACKAGES = new Set(['demo', 'generator-superset']);
  33. function run(cmd, options) {
  34. console.log(`\n>> ${cmd}\n`);
  35. const [p, ...args] = cmd.split(' ');
  36. const runner = spawnSync;
  37. const { status } = runner(p, args, { stdio: 'inherit', ...options });
  38. if (status !== 0) {
  39. process.exit(status);
  40. }
  41. }
  42. function getPackages(packagePattern, tsOnly = false) {
  43. let pattern = packagePattern;
  44. if (pattern === '*' && !tsOnly) {
  45. return `{@superset-ui/!(${[...META_PACKAGES].join('|')}),@apache-superset/*}`;
  46. }
  47. if (!pattern.includes('*')) {
  48. pattern = `*${pattern}`;
  49. }
  50. // Find packages in both @superset-ui and @apache-superset scopes
  51. const supersetUiPackages = [
  52. ...new Set(
  53. fastGlob
  54. .sync([
  55. `./node_modules/@superset-ui/${pattern}/src/**/*.${
  56. tsOnly ? '{ts,tsx}' : '{ts,tsx,js,jsx}'
  57. }`,
  58. ])
  59. .map(x => x.split('/')[3])
  60. .filter(x => !META_PACKAGES.has(x)),
  61. ),
  62. ];
  63. const apachePackages = [
  64. ...new Set(
  65. fastGlob
  66. .sync([
  67. `./node_modules/@apache-superset/${pattern}/src/**/*.${
  68. tsOnly ? '{ts,tsx}' : '{ts,tsx,js,jsx}'
  69. }`,
  70. ])
  71. .map(x => x.split('/')[3]),
  72. ),
  73. ];
  74. const allScopes = [];
  75. if (supersetUiPackages.length > 0) {
  76. allScopes.push(
  77. `@superset-ui/${
  78. supersetUiPackages.length > 1
  79. ? `{${supersetUiPackages.join(',')}}`
  80. : supersetUiPackages[0]
  81. }`,
  82. );
  83. }
  84. if (apachePackages.length > 0) {
  85. allScopes.push(
  86. `@apache-superset/${
  87. apachePackages.length > 1
  88. ? `{${apachePackages.join(',')}}`
  89. : apachePackages[0]
  90. }`,
  91. );
  92. }
  93. if (allScopes.length === 0) {
  94. throw new Error('No matching packages');
  95. }
  96. return allScopes.length > 1 ? `{${allScopes.join(',')}}` : allScopes[0];
  97. }
  98. let scope = getPackages(glob);
  99. console.log('--- Run babel --------');
  100. const babelCommand = `lerna exec --stream --concurrency 10 --scope ${scope}
  101. -- babel ${BABEL_CONFIG} src --extensions ".ts,.tsx,.js,.jsx" --copy-files`;
  102. run(`${babelCommand} --out-dir lib`);
  103. console.log('--- Run babel esm ---');
  104. // run again with
  105. run(`${babelCommand} --out-dir esm`, {
  106. env: { ...process.env, NODE_ENV: 'production', BABEL_OUTPUT: 'esm' },
  107. });
  108. console.log('--- Run tsc ---');
  109. // only run tsc for packages with ts files
  110. scope = getPackages(glob, true);
  111. run(`lerna exec --stream --concurrency 3 --scope ${scope} \
  112. -- ../../scripts/tsc.sh --build`);