Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install script in TS #461

Merged
merged 3 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions install/src/install.js → install/src/install.ts
Original file line number Diff line number Diff line change
@@ -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<string | DirTree>;

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',
Expand All @@ -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',
Expand All @@ -42,24 +53,24 @@ const clean = (pDJ, projName) => {
'readmeFilename',
'repository',
];
const newPackageJson = {};
const newPackageJson: PackageJson = {};

Object.keys(pDJ).forEach(key => {
if (!blacklist.includes(key) && !key.startsWith('_')) {
newPackageJson[key] = pDJ[key];
}
});

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);

Expand All @@ -75,7 +86,7 @@ const rreaddir = root =>
}
});

const isBin = fileAbsPath => {
const isBin = (fileAbsPath: string) => {
switch (path.extname(fileAbsPath)) {
case '.ico':
return true;
Expand All @@ -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<void>((resolve, reject) => {
fs.mkdirSync(dstFileSubfolder, { recursive: true });
const dstFile = fs.createWriteStream(dstFileAbsPath);

Expand Down
11 changes: 2 additions & 9 deletions install/src/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -33,14 +33,7 @@ export default {
{
test: /\.ts$/,
include: path.join(__dirname, '../../'),
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-typescript'],
},
},
],
use: ['babel-loader'],
},
],
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down