-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·74 lines (65 loc) · 2.22 KB
/
index.js
File metadata and controls
executable file
·74 lines (65 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
import { mkdirSync, writeFileSync, existsSync } from 'fs';
import { join } from 'path';
import { validate } from '@prestonarnold/validate';
import { Logger } from '@prestonarnold/log';
const logger = new Logger({ prefix: 'new-typescript-package', showTimestamp: false });
const projectNameArg = process.argv[2];
const result = validate.string().min(1).parse(projectNameArg);
if (!result.success) {
logger.error('Please provide a valid project name.');
logger.info('Usage: npx new-typescript-package my-library');
process.exit(1);
}
const projectName = result.data;
const projectDir = join(process.cwd(), projectName);
if (existsSync(projectDir)) {
logger.error(`Directory "${projectName}" already exists.`);
process.exit(1);
}
mkdirSync(projectDir);
mkdirSync(join(projectDir, 'src'));
const packageJson = {
name: projectName,
version: '0.1.0',
main: 'dist/index.js',
types: 'dist/index.d.ts',
type: 'module',
scripts: {
build: 'tsc',
prepublishOnly: 'npm run build',
test: 'exit 0'
},
files: ["dist", "README.md", "LICENSE"],
license: 'MIT'
};
const tsconfig = {
compilerOptions: {
target: "ES2022",
module: "NodeNext",
moduleResolution: "NodeNext",
outDir: "./dist",
declaration: true,
declarationMap: true,
strict: true,
verbatimModuleSyntax: true,
},
include: ["src/**/*.ts"],
exclude: ["node_modules", "dist"]
};
writeFileSync(join(projectDir, 'package.json'), JSON.stringify(packageJson, null, 2));
writeFileSync(join(projectDir, 'tsconfig.json'), JSON.stringify(tsconfig, null, 2));
writeFileSync(join(projectDir, '.gitignore'), 'node_modules/\ndist/\n');
writeFileSync(join(projectDir, 'README.md'), '# ' + projectName);
writeFileSync(join(projectDir, 'LICENSE'), 'MIT: todo');
writeFileSync(join(projectDir, '.npmignore'), 'node_modules/\nsrc/\n');
writeFileSync(join(projectDir, 'src/index.ts'), `export const hello = () => console.log('Hello from ${projectName}!');\n`);
logger.info("");
logger.info(`Created TypeScript library in ${projectDir}`);
logger.info("");
logger.info(`Next steps:`);
logger.info(` cd ${projectName}`);
logger.info(` npm install --save-dev typescript`);
logger.info(` npm run build`);
logger.info("");
logger.info(`Happy hacking!`);