Skip to content
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
6 changes: 6 additions & 0 deletions .ava-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const path = require('path');

require('ts-node').register({
project: path.resolve('./tsconfig.ava.json'),
transpileOnly: true
});
21 changes: 21 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
push:
branches:
- master

jobs:
publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
cache: 'yarn'
registry-url: 'https://registry.npmjs.org'
- run: yarn install
- run: yarn publish
name: Publish to npm
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ jobs:
node-version: ${{matrix.node_version}}
cache: 'yarn'
- run: yarn install
- run: yarn build
- run: yarn validate
7 changes: 2 additions & 5 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import test from 'ava';
import { promises as fs } from 'fs';
import mkdirp from 'mkdirp';
import path from 'path';
import { render, renderFile, renderGlob, renderToFolder } from '../src';
import { limitOpenFiles } from '../src/utils';
import { render, renderFile, renderGlob, renderToFolder } from '..';

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

return limitOpenFiles(() =>
fs.writeFile(`${templateFolder}/${basename}`, template)
);
return () => fs.writeFile(`${templateFolder}/${basename}`, template);
})
);

Expand Down
33 changes: 13 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "template-file",
"version": "5.1.0",
"version": "6.0.0-0",
"main": "dist/index.js",
"exports": {
"require": "./dist/index.js"
},
"description": "🔀 Replace {{ variables }} in all your files",
"repository": "https://github.com/gsandf/template-file",
"contributors": [
Expand Down Expand Up @@ -38,7 +41,7 @@
"!src/**"
],
"require": [
"ts-node/register"
"./.ava-entry.js"
],
"timeout": "30s"
},
Expand All @@ -50,20 +53,10 @@
"dependencies": {
"@blakek/deep": "^2.2.0",
"glob": "^7.1.6",
"meow": "^8",
"mkdirp": "^1.0.4",
"p-limit": "^3"
"p-limit": "^4.0.0"
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/preset-env": "^7.12.7",
"@babel/preset-typescript": "^7.12.7",
"@rollup/plugin-babel": "^5.2.2",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/plugin-typescript": "^8.0.0",
"@types/glob": "^7.1.3",
"@types/mkdirp": "^1.0.1",
"@typescript-eslint/eslint-plugin": "^5.3.1",
Expand All @@ -74,20 +67,20 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"rollup": "^2.34.0",
"rollup-plugin-terser": "^7.0.2",
"ts-loader": "^9.2.6",
"ts-node": "^10.4.0",
"typescript": "^4.1.2"
"typescript": "^4.1.2",
"webpack": "^5.63.0",
"webpack-cli": "^4.9.1"
},
"peerDependencies": {},
"scripts": {
"build:clean": "rimraf ./dist",
"build:js": "tsc --build",
"build": "run-s build:clean build:js",
"build": "webpack --mode=production",
"build:dev": "webpack --mode=development",
"format-check": "amper-scripts format-check '*.{js,ts,tsx}' 'src/**/*.{js,ts,tsx}'",
"format": "amper-scripts format-write '*.{js,ts,tsx}' 'src/**/*.{js,ts,tsx}'",
"lint": "amper-scripts lint --config ./.eslintrc.js '*.{js,ts,tsx}' 'src/**/*.{js,ts,tsx}'",
"prepack": "run-s validate build",
"prepack": "run-s build validate",
"test": "ava",
"typeCheck": "tsc --noEmit",
"validate": "run-p test format-check lint typeCheck"
Expand Down
54 changes: 42 additions & 12 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#!/usr/bin/env node

import meow from 'meow';
import { promises as fs } from 'fs';
import path from 'path';
import { renderToFolder } from '.';

async function main() {
const cli = meow(
`
/** Trims the leading and trailing whitespace from the help text. */
function trimUsageString(string: string): string {
// Remove newlines from beginning and end
const usage = string.replace(/^(\s*\n)*|(\s*\n)*$/g, '');
// Remove leading indentation
const indentationLength = usage.match(/^\s*/)[0].length;
return usage.replace(new RegExp(`^ {${indentationLength}}`, 'gm'), '');
}

function showUsage() {
const usageString = trimUsageString(`
Usage
$ template-file <dataFile> <sourceGlob> <destination>

Arguments
data Data file in JSON; used to replace variables in source files
dataFile Data file in JSON; used to replace variables in source files
sourceGlob Files to process; see [glob](https://npmjs.com/glob) for syntax
destination Destination directory where processed files go

Expand All @@ -21,15 +27,39 @@ async function main() {

Compile all .abc files in src/ to build/
$ template-file stuff.json 'src/**/*.abc' build/
`
// { importMeta: import.meta }
`);

console.log(usageString);
}

async function getVersion(): Promise<string> {
const packageJson = await fs
.readFile(path.resolve(__dirname, '../package.json'), 'utf-8')
.then(JSON.parse);

return packageJson.version;
}

async function main() {
const args = process.argv.slice(2);

const hasHelpArg = args.some(arg => ['-h', '--help', 'help'].includes(arg));
const hasVersionArg = args.some(arg =>
['-v', '--version', 'version'].includes(arg)
);
const hasCorrectNumberOfArgs = args.length === 3;

if (hasVersionArg) {
console.log(await getVersion());
return;
}

if (cli.input.length !== 3) {
cli.showHelp(2);
if (hasHelpArg || !hasCorrectNumberOfArgs) {
showUsage();
process.exit(hasHelpArg ? 0 : 1);
}

const [dataFile, sourceGlob, destination] = cli.input;
const [dataFile, sourceGlob, destination] = args;
const data = await import(path.resolve(dataFile));

renderToFolder(sourceGlob, destination, data);
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.ava.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"jsx": "react",
"module": "CommonJS",
"target": "ES5"
},
"include": ["__tests__/**/*.ts"],
"exclude": ["node_modules"]
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"declaration": true,
"esModuleInterop": true,
"incremental": true,
"module": "CommonJS",
"module": "ESNext",
"moduleResolution": "node",
"noImplicitAny": true,
"outDir": "dist",
"sourceMap": true,
"target": "ES6",
"target": "ESNext",
"paths": {
"*": ["node_modules/*", "src/types/*"]
}
Expand Down
43 changes: 43 additions & 0 deletions webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const path = require('path');

async function getBaseConfig(): Promise<import('webpack').Configuration> {
const externalDependencies = ['@blakek/deep', 'glob', 'mkdirp'];

const externals = externalDependencies.reduce((externals, packageName) => {
return { ...externals, [packageName]: packageName };
}, {});

return {
entry: {
cli: './src/cli.ts',
index: './src/index.ts'
},

output: {
path: path.resolve(__dirname, 'dist'),
filename: `[name].js`,
library: { type: 'commonjs' },
clean: true
},

externals: externals,

module: {
rules: [
{
test: /\.[jt]sx?$/,
loader: 'ts-loader',
exclude: /node_modules/
}
]
},

resolve: {
extensions: ['.tsx', '.ts', '.js', '.json', '.wasm']
},

target: 'node12'
};
}

module.exports = getBaseConfig();
Loading