Skip to content

Commit c80d611

Browse files
authored
Merge pull request #59 from gsandf/update-build
🔨 update build process, remove dependency on meow
2 parents d3e3052 + a1e20ae commit c80d611

File tree

10 files changed

+587
-1286
lines changed

10 files changed

+587
-1286
lines changed

.ava-entry.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const path = require('path');
2+
3+
require('ts-node').register({
4+
project: path.resolve('./tsconfig.ava.json'),
5+
transpileOnly: true
6+
});

.github/workflows/publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
6+
jobs:
7+
publish:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v2
13+
with:
14+
node-version: '16'
15+
cache: 'yarn'
16+
registry-url: 'https://registry.npmjs.org'
17+
- run: yarn install
18+
- run: yarn publish
19+
name: Publish to npm
20+
env:
21+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

.github/workflows/run-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ jobs:
2323
node-version: ${{matrix.node_version}}
2424
cache: 'yarn'
2525
- run: yarn install
26+
- run: yarn build
2627
- run: yarn validate

__tests__/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import test from 'ava';
22
import { promises as fs } from 'fs';
33
import mkdirp from 'mkdirp';
44
import path from 'path';
5-
import { render, renderFile, renderGlob, renderToFolder } from '../src';
6-
import { limitOpenFiles } from '../src/utils';
5+
import { render, renderFile, renderGlob, renderToFolder } from '..';
76

87
test('Data is replaced when given string', t => {
98
// Should return the same without regard of consistent spacing
@@ -142,9 +141,7 @@ test.skip('Can render a ton of files', async t => {
142141
contents: 'Hello, Test'
143142
});
144143

145-
return limitOpenFiles(() =>
146-
fs.writeFile(`${templateFolder}/${basename}`, template)
147-
);
144+
return () => fs.writeFile(`${templateFolder}/${basename}`, template);
148145
})
149146
);
150147

package.json

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"name": "template-file",
3-
"version": "5.1.0",
3+
"version": "6.0.0-0",
44
"main": "dist/index.js",
5+
"exports": {
6+
"require": "./dist/index.js"
7+
},
58
"description": "🔀 Replace {{ variables }} in all your files",
69
"repository": "https://github.com/gsandf/template-file",
710
"contributors": [
@@ -38,7 +41,7 @@
3841
"!src/**"
3942
],
4043
"require": [
41-
"ts-node/register"
44+
"./.ava-entry.js"
4245
],
4346
"timeout": "30s"
4447
},
@@ -50,20 +53,10 @@
5053
"dependencies": {
5154
"@blakek/deep": "^2.2.0",
5255
"glob": "^7.1.6",
53-
"meow": "^8",
5456
"mkdirp": "^1.0.4",
55-
"p-limit": "^3"
57+
"p-limit": "^4.0.0"
5658
},
5759
"devDependencies": {
58-
"@babel/core": "^7.12.9",
59-
"@babel/plugin-proposal-class-properties": "^7.12.1",
60-
"@babel/plugin-transform-runtime": "^7.12.1",
61-
"@babel/preset-env": "^7.12.7",
62-
"@babel/preset-typescript": "^7.12.7",
63-
"@rollup/plugin-babel": "^5.2.2",
64-
"@rollup/plugin-commonjs": "^21.0.1",
65-
"@rollup/plugin-node-resolve": "^13.0.6",
66-
"@rollup/plugin-typescript": "^8.0.0",
6760
"@types/glob": "^7.1.3",
6861
"@types/mkdirp": "^1.0.1",
6962
"@typescript-eslint/eslint-plugin": "^5.3.1",
@@ -74,20 +67,20 @@
7467
"npm-run-all": "^4.1.5",
7568
"prettier": "^2.2.1",
7669
"rimraf": "^3.0.2",
77-
"rollup": "^2.34.0",
78-
"rollup-plugin-terser": "^7.0.2",
70+
"ts-loader": "^9.2.6",
7971
"ts-node": "^10.4.0",
80-
"typescript": "^4.1.2"
72+
"typescript": "^4.1.2",
73+
"webpack": "^5.63.0",
74+
"webpack-cli": "^4.9.1"
8175
},
8276
"peerDependencies": {},
8377
"scripts": {
84-
"build:clean": "rimraf ./dist",
85-
"build:js": "tsc --build",
86-
"build": "run-s build:clean build:js",
78+
"build": "webpack --mode=production",
79+
"build:dev": "webpack --mode=development",
8780
"format-check": "amper-scripts format-check '*.{js,ts,tsx}' 'src/**/*.{js,ts,tsx}'",
8881
"format": "amper-scripts format-write '*.{js,ts,tsx}' 'src/**/*.{js,ts,tsx}'",
8982
"lint": "amper-scripts lint --config ./.eslintrc.js '*.{js,ts,tsx}' 'src/**/*.{js,ts,tsx}'",
90-
"prepack": "run-s validate build",
83+
"prepack": "run-s build validate",
9184
"test": "ava",
9285
"typeCheck": "tsc --noEmit",
9386
"validate": "run-p test format-check lint typeCheck"

src/cli.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
#!/usr/bin/env node
2-
3-
import meow from 'meow';
1+
import { promises as fs } from 'fs';
42
import path from 'path';
53
import { renderToFolder } from '.';
64

7-
async function main() {
8-
const cli = meow(
9-
`
5+
/** Trims the leading and trailing whitespace from the help text. */
6+
function trimUsageString(string: string): string {
7+
// Remove newlines from beginning and end
8+
const usage = string.replace(/^(\s*\n)*|(\s*\n)*$/g, '');
9+
// Remove leading indentation
10+
const indentationLength = usage.match(/^\s*/)[0].length;
11+
return usage.replace(new RegExp(`^ {${indentationLength}}`, 'gm'), '');
12+
}
13+
14+
function showUsage() {
15+
const usageString = trimUsageString(`
1016
Usage
1117
$ template-file <dataFile> <sourceGlob> <destination>
1218
1319
Arguments
14-
data Data file in JSON; used to replace variables in source files
20+
dataFile Data file in JSON; used to replace variables in source files
1521
sourceGlob Files to process; see [glob](https://npmjs.com/glob) for syntax
1622
destination Destination directory where processed files go
1723
@@ -21,15 +27,39 @@ async function main() {
2127
2228
Compile all .abc files in src/ to build/
2329
$ template-file stuff.json 'src/**/*.abc' build/
24-
`
25-
// { importMeta: import.meta }
30+
`);
31+
32+
console.log(usageString);
33+
}
34+
35+
async function getVersion(): Promise<string> {
36+
const packageJson = await fs
37+
.readFile(path.resolve(__dirname, '../package.json'), 'utf-8')
38+
.then(JSON.parse);
39+
40+
return packageJson.version;
41+
}
42+
43+
async function main() {
44+
const args = process.argv.slice(2);
45+
46+
const hasHelpArg = args.some(arg => ['-h', '--help', 'help'].includes(arg));
47+
const hasVersionArg = args.some(arg =>
48+
['-v', '--version', 'version'].includes(arg)
2649
);
50+
const hasCorrectNumberOfArgs = args.length === 3;
51+
52+
if (hasVersionArg) {
53+
console.log(await getVersion());
54+
return;
55+
}
2756

28-
if (cli.input.length !== 3) {
29-
cli.showHelp(2);
57+
if (hasHelpArg || !hasCorrectNumberOfArgs) {
58+
showUsage();
59+
process.exit(hasHelpArg ? 0 : 1);
3060
}
3161

32-
const [dataFile, sourceGlob, destination] = cli.input;
62+
const [dataFile, sourceGlob, destination] = args;
3363
const data = await import(path.resolve(dataFile));
3464

3565
renderToFolder(sourceGlob, destination, data);

tsconfig.ava.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"jsx": "react",
5+
"module": "CommonJS",
6+
"target": "ES5"
7+
},
8+
"include": ["__tests__/**/*.ts"],
9+
"exclude": ["node_modules"]
10+
}

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"declaration": true,
66
"esModuleInterop": true,
77
"incremental": true,
8-
"module": "CommonJS",
8+
"module": "ESNext",
99
"moduleResolution": "node",
1010
"noImplicitAny": true,
1111
"outDir": "dist",
1212
"sourceMap": true,
13-
"target": "ES6",
13+
"target": "ESNext",
1414
"paths": {
1515
"*": ["node_modules/*", "src/types/*"]
1616
}

webpack.config.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const path = require('path');
2+
3+
async function getBaseConfig(): Promise<import('webpack').Configuration> {
4+
const externalDependencies = ['@blakek/deep', 'glob', 'mkdirp'];
5+
6+
const externals = externalDependencies.reduce((externals, packageName) => {
7+
return { ...externals, [packageName]: packageName };
8+
}, {});
9+
10+
return {
11+
entry: {
12+
cli: './src/cli.ts',
13+
index: './src/index.ts'
14+
},
15+
16+
output: {
17+
path: path.resolve(__dirname, 'dist'),
18+
filename: `[name].js`,
19+
library: { type: 'commonjs' },
20+
clean: true
21+
},
22+
23+
externals: externals,
24+
25+
module: {
26+
rules: [
27+
{
28+
test: /\.[jt]sx?$/,
29+
loader: 'ts-loader',
30+
exclude: /node_modules/
31+
}
32+
]
33+
},
34+
35+
resolve: {
36+
extensions: ['.tsx', '.ts', '.js', '.json', '.wasm']
37+
},
38+
39+
target: 'node12'
40+
};
41+
}
42+
43+
module.exports = getBaseConfig();

0 commit comments

Comments
 (0)