diff --git a/install/src/install.js b/install/src/install.ts similarity index 74% rename from install/src/install.js rename to install/src/install.ts index c2ac4998..758a8270 100755 --- a/install/src/install.js +++ b/install/src/install.ts @@ -1,21 +1,28 @@ #!/usr/bin/env node /* eslint-env { parserOptions: { ecmaVersion: 8 }, env: { node: true } } */ -/* eslint-disable global-require, import/no-dynamic-require, import/no-unresolved, @typescript-eslint/no-var-requires */ -const fs = require('fs'); -const path = require('path'); -const { execSync } = require('child_process'); -const { Transform } = require('stream'); -const { promisify } = require('util'); -const replaceStream = require('replacestream'); +import fs, { EncodingOption } from 'fs'; +import { Blob } from 'buffer'; +import path from 'path'; +import { execSync } from 'child_process'; +import { Transform } from 'stream'; +import { promisify } from 'util'; +import replaceStream from 'replacestream'; +import { JSONSchemaForNPMPackageJsonFiles as PackageJson } from '@schemastore/package'; -const noopTransform = (chunk, encoding, callback) => { +import config from '../../config'; + +type DirTree = Array; + +const noopTransform = ( + chunk: Blob, + encoding: EncodingOption, + callback: (error: Error | undefined, nextChunk: Blob) => void, +) => { callback(undefined, chunk); }; -const config = require('../../config').default; - const ignorePaths = [ '.git', '.npmignore', @@ -27,9 +34,13 @@ const ignorePaths = [ '.yarn', ].map(x => path.resolve(__dirname, x)); -const flatten = list => list.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []); +const flatten = (tree: DirTree): string[] => + tree.reduce( + (accum: string[], curr: string | DirTree) => accum.concat(Array.isArray(curr) ? flatten(curr) : curr), + [], + ); -const clean = (pDJ, projName) => { +const clean = (pDJ: PackageJson, projName: string) => { const blacklist = [ 'bin', 'bugs', @@ -42,7 +53,7 @@ const clean = (pDJ, projName) => { 'readmeFilename', 'repository', ]; - const newPackageJson = {}; + const newPackageJson: PackageJson = {}; Object.keys(pDJ).forEach(key => { if (!blacklist.includes(key) && !key.startsWith('_')) { @@ -50,16 +61,16 @@ const clean = (pDJ, projName) => { } }); - delete newPackageJson.scripts.postinstall; - delete newPackageJson.scripts.prepublish; - newPackageJson.scripts.test = 'jest -c app/jest.config.ts'; + delete newPackageJson.scripts!.postinstall; + delete newPackageJson.scripts!.prepublish; + newPackageJson.scripts!.test = 'jest -c app/jest.config.ts'; newPackageJson.name = projName; return JSON.stringify(newPackageJson, null, 2); }; -const rreaddir = root => +const rreaddir = (root: string): DirTree => fs.readdirSync(root).map(file => { const fileAbsPath = path.join(root, file); @@ -75,7 +86,7 @@ const rreaddir = root => } }); -const isBin = fileAbsPath => { +const isBin = (fileAbsPath: string) => { switch (path.extname(fileAbsPath)) { case '.ico': return true; @@ -96,7 +107,7 @@ const isBin = fileAbsPath => { const dstFileAbsPath = path.join(newProjectRoot, srcFileRelFromSrcRoot); const dstFileSubfolder = path.parse(dstFileAbsPath).dir; - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { fs.mkdirSync(dstFileSubfolder, { recursive: true }); const dstFile = fs.createWriteStream(dstFileAbsPath); diff --git a/install/src/webpack.config.ts b/install/src/webpack.config.ts index b8e23f3e..7fc0ca02 100644 --- a/install/src/webpack.config.ts +++ b/install/src/webpack.config.ts @@ -4,7 +4,7 @@ import TerserPlugin from 'terser-webpack-plugin'; // webpack is the wrong tool for this job - I'm only using it because I refuse to let the dep list for this project grow any more export default { entry: { - main: path.resolve(__dirname, 'install.js'), + main: path.resolve(__dirname, 'install.ts'), }, output: { filename: 'index.js', @@ -33,14 +33,7 @@ export default { { test: /\.ts$/, include: path.join(__dirname, '../../'), - use: [ - { - loader: 'babel-loader', - options: { - presets: ['@babel/preset-typescript'], - }, - }, - ], + use: ['babel-loader'], }, ], }, diff --git a/package.json b/package.json index f4d81bac..4a2533f4 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@bjacobel/vhtml-loader": "^2.3.0", "@linaria/stylelint-config-standard-linaria": "^6.2.0", "@mapbox/cloudfriend": "^8.1.0", + "@schemastore/package": "^0.0.10", "@testing-library/dom": "^10.0.0", "@testing-library/jest-dom": "^6.0.0", "@testing-library/react": "^15.0.0", diff --git a/tsconfig.json b/tsconfig.json index 2873a318..a09c3797 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,5 +17,5 @@ "module": "CommonJS" } }, - "include": ["app/**/*", "deploy/**/*", "node_modules/@testing-library/jest-dom/types/jest.d.ts"] + "include": ["app/**/*", "deploy/**/*", "install/src/**/*", "node_modules/@testing-library/jest-dom/types/jest.d.ts"] } diff --git a/yarn.lock b/yarn.lock index b89c1b5e..8fde930e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2918,6 +2918,11 @@ estree-walker "^2.0.2" picomatch "^2.3.1" +"@schemastore/package@^0.0.10": + version "0.0.10" + resolved "https://registry.yarnpkg.com/@schemastore/package/-/package-0.0.10.tgz#86e96575328ecf6d0e9299aa9d7ea28afdd8058a" + integrity sha512-D3LxMCnkgsb4LO5sDKf6E+yahM2SqpEHmkqMPDSJis5Cy/j2MgWo/g/iq0lECK0mrPWfx3hqKm2ZJlqxwbRJQA== + "@sinclair/typebox@^0.27.8": version "0.27.8" resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e"