From 580825d7ae811ea68d3715e0e5219cb917f15274 Mon Sep 17 00:00:00 2001 From: Jason Morse <35609178+JasonVMo@users.noreply.github.com> Date: Wed, 19 Aug 2020 15:52:37 -0700 Subject: [PATCH] Make the scripts buildable to allow typescript within (#403) * move scripts to be building via TypeScript * convert scripts to build using typescript * move babel up to scripts root for direct inclusion * remove unused use of depcheck task from build-tools --- lage.config.js | 5 +- packages/utils/build-tools/just.config.js | 6 +- scripts/babel.config.js | 12 +-- scripts/eslintrc.js | 45 --------- scripts/index.js | 6 +- scripts/just.config.js | 99 ++++--------------- scripts/package.json | 9 +- scripts/{ => src}/clean-all.js | 0 scripts/{ => src}/debug-metro-pack.js | 0 scripts/src/eslintPreset.ts | 41 ++++++++ scripts/src/index.ts | 3 + scripts/src/justPreset.ts | 88 +++++++++++++++++ scripts/{ => src}/tasks/api-extractor.js | 0 .../tasks/check-for-modified-files.js | 0 scripts/{ => src}/tasks/clean.js | 0 scripts/{ => src}/tasks/copy.js | 0 scripts/{ => src}/tasks/depcheck.js | 0 scripts/{ => src}/tasks/eslint.js | 0 scripts/{ => src}/tasks/jest.js | 0 scripts/{ => src}/tasks/ts.js | 0 scripts/{ => src}/tasks/webpack.js | 0 .../{ => src}/webpack/webpack-resources.js | 0 scripts/tasks/prettier.js | 7 -- scripts/test.js | 29 ------ scripts/tsconfig.json | 4 +- 25 files changed, 170 insertions(+), 184 deletions(-) delete mode 100644 scripts/eslintrc.js rename scripts/{ => src}/clean-all.js (100%) rename scripts/{ => src}/debug-metro-pack.js (100%) create mode 100644 scripts/src/eslintPreset.ts create mode 100644 scripts/src/index.ts create mode 100644 scripts/src/justPreset.ts rename scripts/{ => src}/tasks/api-extractor.js (100%) rename scripts/{ => src}/tasks/check-for-modified-files.js (100%) rename scripts/{ => src}/tasks/clean.js (100%) rename scripts/{ => src}/tasks/copy.js (100%) rename scripts/{ => src}/tasks/depcheck.js (100%) rename scripts/{ => src}/tasks/eslint.js (100%) rename scripts/{ => src}/tasks/jest.js (100%) rename scripts/{ => src}/tasks/ts.js (100%) rename scripts/{ => src}/tasks/webpack.js (100%) rename scripts/{ => src}/webpack/webpack-resources.js (100%) delete mode 100644 scripts/tasks/prettier.js delete mode 100644 scripts/test.js diff --git a/lage.config.js b/lage.config.js index 2c54a35faa..2fc13f2fdb 100644 --- a/lage.config.js +++ b/lage.config.js @@ -1,4 +1,5 @@ module.exports = { + npmClient: 'yarn', pipeline: { ['build-tools']: ['^build-tools'], build: ['build-tools', '^build'], @@ -9,6 +10,6 @@ module.exports = { lint: ['build-tools'], ['verify-api']: [], ['update-api']: [], - test: ['build-tools', 'lint', 'build'] - } + test: ['build-tools', 'lint', 'build'], + }, }; diff --git a/packages/utils/build-tools/just.config.js b/packages/utils/build-tools/just.config.js index d8ee1a74cb..0314979a9c 100644 --- a/packages/utils/build-tools/just.config.js +++ b/packages/utils/build-tools/just.config.js @@ -1,7 +1,6 @@ // @ts-check const { task, series, parallel, option, argv, tscTask, cleanTask, eslintTask } = require('just-scripts'); -const { depcheckTask } = require('@uifabricshared/build-native/tasks/depcheck'); const path = require('path'); @@ -18,8 +17,8 @@ module.exports = function preset() { target: 'es6', outDir: 'lib', module: 'commonjs', - ...(argv().production && { inlineSources: true, sourceRoot: path.relative(libPath, srcPath) }) - }) + ...(argv().production && { inlineSources: true, sourceRoot: path.relative(libPath, srcPath) }), + }), ); task('lint', eslintTask({ files: ['src/.'] })); @@ -27,5 +26,4 @@ module.exports = function preset() { task('build', series('cleanlib', parallel('lint', 'ts'))); task('no-op', () => {}); task('clean', 'no-op'); - task('depcheck', depcheckTask); }; diff --git a/scripts/babel.config.js b/scripts/babel.config.js index 37cd1c60f6..0e39a7d44d 100644 --- a/scripts/babel.config.js +++ b/scripts/babel.config.js @@ -5,7 +5,7 @@ module.exports = { ['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-react', '@babel/preset-typescript', - 'module:metro-react-native-babel-preset' + 'module:metro-react-native-babel-preset', ], plugins: [ [ @@ -37,9 +37,9 @@ module.exports = { '.win32.tsx', '.windows.js', '.windows.ts', - '.windows.tsx' - ] - } - ] - ] + '.windows.tsx', + ], + }, + ], + ], }; diff --git a/scripts/eslintrc.js b/scripts/eslintrc.js deleted file mode 100644 index 8120988b54..0000000000 --- a/scripts/eslintrc.js +++ /dev/null @@ -1,45 +0,0 @@ -module.exports = { - extends: [ - 'plugin:@typescript-eslint/recommended' - ], - parser: '@typescript-eslint/parser', - plugins: [ - '@typescript-eslint', - ], - rules: { - "@typescript-eslint/adjacent-overload-signatures": "off", - "@typescript-eslint/ban-ts-ignore": "off", - "@typescript-eslint/ban-types": "off", - "camelcase": "off", - "@typescript-eslint/camelcase": "off", - "@typescript-eslint/class-name-casing": "off", - "@typescript-eslint/consistent-type-assertions": "off", - "@typescript-eslint/explicit-function-return-type": "off", - "@typescript-eslint/interface-name-prefix": "off", - "@typescript-eslint/member-delimiter-style": "off", - "no-array-constructor": "off", - "@typescript-eslint/no-array-constructor": "off", - "no-empty-function": "off", - "@typescript-eslint/no-empty-function": "off", - "@typescript-eslint/no-empty-interface": "off", - "@typescript-eslint/no-explicit-any": "off", - "@typescript-eslint/no-inferrable-types": "off", - "@typescript-eslint/no-misused-new": "off", - "@typescript-eslint/no-namespace": "off", - "@typescript-eslint/no-non-null-assertion": "off", - "@typescript-eslint/no-this-alias": "off", - "no-unused-vars": "off", - "@typescript-eslint/no-unused-vars": "off", - "no-use-before-define": "off", - "@typescript-eslint/no-use-before-define": "off", - "@typescript-eslint/no-var-requires": "off", - "@typescript-eslint/prefer-namespace-keyword": "off", - "@typescript-eslint/triple-slash-reference": "off", - "@typescript-eslint/type-annotation-spacing": "off", - "no-var": "off", - "prefer-const": "off", - "prefer-rest-params": "off", - "prefer-spread": "off", - "no-undef": "error" - } -} \ No newline at end of file diff --git a/scripts/index.js b/scripts/index.js index bfd614ef73..13ff3b161c 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -1,6 +1,6 @@ module.exports = { - preset: require('./just.config'), + preset: require('./lib/justPreset'), just: require('just-scripts'), - eslintPreset: require('./eslintrc'), - ...require('@fluentui-react-native/build-tools') + eslintPreset: require('./lib/eslintrc'), + ...require('@fluentui-react-native/build-tools'), }; diff --git a/scripts/just.config.js b/scripts/just.config.js index 7838c1c91d..502c44d9dd 100644 --- a/scripts/just.config.js +++ b/scripts/just.config.js @@ -1,95 +1,30 @@ // @ts-check -const { task, series, parallel, condition, option, argv, addResolvePath, prettierCheckTask, prettierTask } = require('just-scripts'); +const { task, series, parallel, option, argv, tscTask, cleanTask, eslintTask } = require('just-scripts'); const path = require('path'); -const fs = require('fs'); -const { clean } = require('./tasks/clean'); -const { copy } = require('./tasks/copy'); -const { jest } = require('./tasks/jest'); -const { ts } = require('./tasks/ts'); -const { eslint } = require('./tasks/eslint'); -const { webpack, webpackDevServer } = require('./tasks/webpack'); -const { metroTask } = require('@fluentui-react-native/build-tools'); -const { verifyApiExtractor, updateApiExtractor } = require('./tasks/api-extractor'); -const checkForModifiedFiles = require('./tasks/check-for-modified-files'); -const { depcheckTask } = require('./tasks/depcheck'); - -function fileExists(path) { - try { - return fs.existsSync(path); - } catch { - return false; - } -} +const srcPath = path.join(process.cwd(), 'src'); +const libPath = path.join(process.cwd(), 'lib'); module.exports = function preset() { - // this add s a resolve path for the build tooling deps like TS from the scripts folder - addResolvePath(__dirname); - option('production'); - // Adds an alias for 'npm-install-mode' for backwards compatibility - option('min', { alias: 'npm-install-mode' }); - - option('webpackConfig', { alias: 'w' }); - - // Build only commonjs (not other TS variants) but still run other tasks - option('commonjs'); - - // use Metro for bundling task instead of the default webpack - option('useMetro'); - option('dev'); - option('platform', { type: 'string' }); - option('bundleName', { type: 'string' }); - option('server'); - option('port', { type: 'number' }); - option('cli'); - - // for options that have a check/fix switch this puts them into fix mode - option('fix'); - - task('clean', clean); - task('copy', copy); - task('jest', jest); - task('ts:commonjs', ts.commonjs); - task('ts:esm', ts.esm); - task('eslint', eslint); - task('ts:commonjs-only', ts.commonjsOnly); - task('webpack', webpack); - task('webpack-dev-server', webpackDevServer); - task('verify-api-extractor', verifyApiExtractor); - task('update-api-extractor', updateApiExtractor); - task('prettier', () => (argv().fix ? prettierTask : prettierCheckTask)); - task('check-for-modified-files', checkForModifiedFiles); - task('tsall', parallel('ts:commonjs', 'ts:esm')); - task('ts', series(condition('ts:commonjs-only', () => !!argv().commonjs), condition('tsall', () => !argv().commonjs))); - - task('validate', parallel('eslint', condition('jest', () => fileExists(path.join(process.cwd(), 'jest.config.js'))))); - - task('code-style', series('prettier', 'eslint')); - task('update-api', series('clean', 'copy', 'ts', 'update-api-extractor')); - task('dev', series('clean', 'copy', 'webpack-dev-server')); - - task('build:node-lib', series('clean', 'copy', series(condition('validate', () => !argv().min), 'ts:commonjs-only'))); - - task('metro', () => - metroTask({ - dev: !!argv().dev, - ...(argv().cli && { cli: true }), - ...(argv().platform && { platform: argv().platform }), - ...(argv().bundleName && { bundleName: argv().bundleName }), - ...(argv().server && { server: true }), - ...(argv().server && argv().port && { port: argv().port }) - }) + task( + 'ts', + tscTask({ + pretty: true, + allowJs: true, + target: 'es6', + outDir: 'lib', + module: 'commonjs', + ...(argv().production && { inlineSources: true, sourceRoot: path.relative(libPath, srcPath) }), + }), ); - task('bundle', series(condition('metro', () => !!argv().useMetro), condition('webpack', () => !argv().useMetro))); - - task('build', series('clean', 'copy', 'ts')); - - task('depcheck', depcheckTask); - + task('lint', eslintTask({ files: ['src/.'] })); + task('cleanlib', cleanTask([libPath])); + task('build', series('cleanlib', parallel('lint', 'ts'))); task('no-op', () => {}); + task('clean', 'no-op'); }; diff --git a/scripts/package.json b/scripts/package.json index 94b10bbe1c..c4ffd910ac 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -2,16 +2,15 @@ "name": "@uifabricshared/build-native", "version": "0.1.1", "private": true, - "main": "./index.js", + "main": "./lib/index.js", "bin": { "fluentui-scripts": "./just-scripts.js" }, "scripts": { - "build": "node ./just-scripts.js no-op", + "build-tools": "just-scripts build", "bundlesize": "bundlesize --debug", - "clean": "node ./just-scripts.js no-op", - "code-style": "node ./just-scripts.js code-style", - "prettier": "node ./just-scripts.js prettier", + "code-style": "node ./lib/just-scripts.js code-style", + "prettier": "node ./lib/just-scripts.js prettier", "verify-api": "echo no api to verify", "update-api": "echo no api to update" }, diff --git a/scripts/clean-all.js b/scripts/src/clean-all.js similarity index 100% rename from scripts/clean-all.js rename to scripts/src/clean-all.js diff --git a/scripts/debug-metro-pack.js b/scripts/src/debug-metro-pack.js similarity index 100% rename from scripts/debug-metro-pack.js rename to scripts/src/debug-metro-pack.js diff --git a/scripts/src/eslintPreset.ts b/scripts/src/eslintPreset.ts new file mode 100644 index 0000000000..28baf5cbcf --- /dev/null +++ b/scripts/src/eslintPreset.ts @@ -0,0 +1,41 @@ +export const eslintPreset = { + extends: ['plugin:@typescript-eslint/recommended'], + parser: '@typescript-eslint/parser', + plugins: ['@typescript-eslint'], + rules: { + '@typescript-eslint/adjacent-overload-signatures': 'off', + '@typescript-eslint/ban-ts-ignore': 'off', + '@typescript-eslint/ban-types': 'off', + camelcase: 'off', + '@typescript-eslint/camelcase': 'off', + '@typescript-eslint/class-name-casing': 'off', + '@typescript-eslint/consistent-type-assertions': 'off', + '@typescript-eslint/explicit-function-return-type': 'off', + '@typescript-eslint/interface-name-prefix': 'off', + '@typescript-eslint/member-delimiter-style': 'off', + 'no-array-constructor': 'off', + '@typescript-eslint/no-array-constructor': 'off', + 'no-empty-function': 'off', + '@typescript-eslint/no-empty-function': 'off', + '@typescript-eslint/no-empty-interface': 'off', + '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/no-inferrable-types': 'off', + '@typescript-eslint/no-misused-new': 'off', + '@typescript-eslint/no-namespace': 'off', + '@typescript-eslint/no-non-null-assertion': 'off', + '@typescript-eslint/no-this-alias': 'off', + 'no-unused-vars': 'off', + '@typescript-eslint/no-unused-vars': 'off', + 'no-use-before-define': 'off', + '@typescript-eslint/no-use-before-define': 'off', + '@typescript-eslint/no-var-requires': 'off', + '@typescript-eslint/prefer-namespace-keyword': 'off', + '@typescript-eslint/triple-slash-reference': 'off', + '@typescript-eslint/type-annotation-spacing': 'off', + 'no-var': 'off', + 'prefer-const': 'off', + 'prefer-rest-params': 'off', + 'prefer-spread': 'off', + 'no-undef': 'error', + }, +}; diff --git a/scripts/src/index.ts b/scripts/src/index.ts new file mode 100644 index 0000000000..e2eb1b2e7a --- /dev/null +++ b/scripts/src/index.ts @@ -0,0 +1,3 @@ +export * from './justPreset'; +export * from '@fluentui-react-native/build-tools'; +export * from './eslintPreset'; diff --git a/scripts/src/justPreset.ts b/scripts/src/justPreset.ts new file mode 100644 index 0000000000..f34fea6700 --- /dev/null +++ b/scripts/src/justPreset.ts @@ -0,0 +1,88 @@ +// @ts-check + +const { task, series, parallel, condition, option, argv, addResolvePath, prettierCheckTask, prettierTask } = require('just-scripts'); + +const path = require('path'); +const fs = require('fs'); + +const { clean } = require('./tasks/clean'); +const { copy } = require('./tasks/copy'); +const { jest } = require('./tasks/jest'); +const { ts } = require('./tasks/ts'); +const { eslint } = require('./tasks/eslint'); +const { webpack, webpackDevServer } = require('./tasks/webpack'); +const { depcheckTask } = require('./tasks/depcheck'); +const { verifyApiExtractor, updateApiExtractor } = require('./tasks/api-extractor'); +const checkForModifiedFiles = require('./tasks/check-for-modified-files'); + +import { metroTask } from '@fluentui-react-native/build-tools'; + +export function preset() { + // this add s a resolve path for the build tooling deps like TS from the scripts folder + addResolvePath(__dirname); + + option('production'); + + // Adds an alias for 'npm-install-mode' for backwards compatibility + option('min', { alias: 'npm-install-mode' }); + + option('webpackConfig', { alias: 'w' }); + + // Build only commonjs (not other TS variants) but still run other tasks + option('commonjs'); + + // use Metro for bundling task instead of the default webpack + option('useMetro'); + option('dev'); + option('platform', { string: true }); + option('bundleName', { string: true }); + option('server'); + option('port', { number: true }); + option('cli'); + + // for options that have a check/fix switch this puts them into fix mode + option('fix'); + + task('clean', clean); + task('copy', copy); + task('jest', jest); + task('ts:commonjs', ts.commonjs); + task('ts:esm', ts.esm); + task('eslint', eslint); + task('ts:commonjs-only', ts.commonjsOnly); + task('webpack', webpack); + task('webpack-dev-server', webpackDevServer); + task('verify-api-extractor', verifyApiExtractor); + task('update-api-extractor', updateApiExtractor); + task('prettier', () => (argv().fix ? prettierTask : prettierCheckTask)); + task('check-for-modified-files', checkForModifiedFiles); + task('tsall', parallel('ts:commonjs', 'ts:esm')); + task('ts', series(condition('ts:commonjs-only', () => !!argv().commonjs), condition('tsall', () => !argv().commonjs))); + + task('validate', parallel('eslint', condition('jest', () => fs.existsSync(path.join(process.cwd(), 'jest.config.js'))))); + + task('code-style', series('prettier', 'eslint')); + task('update-api', series('clean', 'copy', 'ts', 'update-api-extractor')); + task('dev', series('clean', 'copy', 'webpack-dev-server')); + + task('build:node-lib', series('clean', 'copy', series(condition('validate', () => !argv().min), 'ts:commonjs-only'))); + + task('metro', () => + metroTask({ + dev: !!argv().dev, + ...(argv().cli && { cli: true }), + ...(argv().platform && { platform: argv().platform }), + ...(argv().bundleName && { bundleName: argv().bundleName }), + ...(argv().server && { server: true }), + ...(argv().server && argv().port && { port: argv().port }), + }), + ); + + task('bundle', series(condition('metro', () => !!argv().useMetro), condition('webpack', () => !argv().useMetro))); + + task('build', series('clean', 'copy', 'ts')); + + task('depcheck', depcheckTask); + + task('no-op', () => {}); +} diff --git a/scripts/tasks/api-extractor.js b/scripts/src/tasks/api-extractor.js similarity index 100% rename from scripts/tasks/api-extractor.js rename to scripts/src/tasks/api-extractor.js diff --git a/scripts/tasks/check-for-modified-files.js b/scripts/src/tasks/check-for-modified-files.js similarity index 100% rename from scripts/tasks/check-for-modified-files.js rename to scripts/src/tasks/check-for-modified-files.js diff --git a/scripts/tasks/clean.js b/scripts/src/tasks/clean.js similarity index 100% rename from scripts/tasks/clean.js rename to scripts/src/tasks/clean.js diff --git a/scripts/tasks/copy.js b/scripts/src/tasks/copy.js similarity index 100% rename from scripts/tasks/copy.js rename to scripts/src/tasks/copy.js diff --git a/scripts/tasks/depcheck.js b/scripts/src/tasks/depcheck.js similarity index 100% rename from scripts/tasks/depcheck.js rename to scripts/src/tasks/depcheck.js diff --git a/scripts/tasks/eslint.js b/scripts/src/tasks/eslint.js similarity index 100% rename from scripts/tasks/eslint.js rename to scripts/src/tasks/eslint.js diff --git a/scripts/tasks/jest.js b/scripts/src/tasks/jest.js similarity index 100% rename from scripts/tasks/jest.js rename to scripts/src/tasks/jest.js diff --git a/scripts/tasks/ts.js b/scripts/src/tasks/ts.js similarity index 100% rename from scripts/tasks/ts.js rename to scripts/src/tasks/ts.js diff --git a/scripts/tasks/webpack.js b/scripts/src/tasks/webpack.js similarity index 100% rename from scripts/tasks/webpack.js rename to scripts/src/tasks/webpack.js diff --git a/scripts/webpack/webpack-resources.js b/scripts/src/webpack/webpack-resources.js similarity index 100% rename from scripts/webpack/webpack-resources.js rename to scripts/src/webpack/webpack-resources.js diff --git a/scripts/tasks/prettier.js b/scripts/tasks/prettier.js deleted file mode 100644 index 455a7b51ec..0000000000 --- a/scripts/tasks/prettier.js +++ /dev/null @@ -1,7 +0,0 @@ -// @ts-check - -const { runPrettierForProject } = require('../prettier/prettier-helpers'); - -module.exports = function() { - runPrettierForProject(process.cwd()); -}; diff --git a/scripts/test.js b/scripts/test.js deleted file mode 100644 index 11b43547dc..0000000000 --- a/scripts/test.js +++ /dev/null @@ -1,29 +0,0 @@ -const { getPackageInfo } = require('just-repo-utils'); -const fs = require('fs'); - -function testStuff(strategy) { - const options = strategy ? { strategy } : {}; - return [ - getPackageInfo(options).names(), - getPackageInfo(options).paths(), - getPackageInfo(options) - .dependencies({ ...options, target: '@uifabricshared/foundation-compose' }) - .names(), - getPackageInfo(options) - .dependencies({ ...options, target: '@fluentui-react-native/immutable-merge' }) - .names(), - getPackageInfo(options) - .dependencies({ ...options, target: 'foo' }) - .names() - ]; -} - -function testSet(strategy) { - console.time('test'); - const results = testStuff(strategy); - console.timeEnd('test'); - results.forEach(result => console.log(result)); -} - -testSet('no-cache'); -testSet(); diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json index d009ed2b02..3f9c9eb91d 100644 --- a/scripts/tsconfig.json +++ b/scripts/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "outDir": "./lib", "target": "es2015", "module": "commonjs", "moduleResolution": "node", @@ -23,5 +24,6 @@ "baseUrl": ".", "paths": { "*": ["src/*"] - } + }, + "include": ["src"] }