-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
114 lines (93 loc) · 3.29 KB
/
generate.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env node
const { execSync } = require('child_process')
const readline = require('readline')
const util = require('util')
const fs = require('fs-extra')
const packageJson = require('./package.json')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const question = util.promisify(rl.question).bind(rl)
// eslint-disable-next-line no-extend-native
String.prototype.toKebabCase = function () {
return this.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => '-' + chr).trim()
}
// main
async function main () {
const scope = await question('Enter Scope (default: @stephen-shopopop):') || 'stephen-shopopop'
const scopeName = scope.toKebabCase()
const name = await question('What is the name of the package ? ')
const packageName = name.toKebabCase()
const cmd = `npm init --scope=@${scopeName} -y -w ./packages/${packageName}`
execSync(cmd.toString(), {
cwd: __dirname,
stdio: 'inherit'
})
// Generate npmignore
await fs.outputFile(`./packages/${packageName}/.npmignore`, `*
!dist/**
!package.json
!readme.md
`)
// Generate tsconfig for package
await fs.writeJSON(`./packages/${packageName}/tsconfig.pkg.json`, {
extends: '../../tsconfig.build.json',
compilerOptions: {
outDir: './dist/'
},
include: [
'./**/*.ts'
],
exclude: [
'**/*.test.ts'
]
}, { spaces: 2 })
// Generate readme.md
await fs.outputFile(`./packages/${packageName}/readme.md`, `[![Minimal node version](https://img.shields.io/static/v1?label=node&message=${packageJson.engines.node}&logo=node.js&color)](https://nodejs.org/about/releases/)
[![Minimal npm version](https://img.shields.io/static/v1?label=npm&message=${packageJson.engines.npm}&logo=npm&color)](https://github.com/npm/cli/releases)
[![Linux](https://svgshare.com/i/Zhy.svg)](https://svgshare.com/i/Zhy.svg)
[![macOS](https://svgshare.com/i/ZjP.svg)](https://svgshare.com/i/ZjP.svg)
[![Visual Studio Code](https://img.shields.io/badge/--007ACC?logo=visual%20studio%20code&logoColor=ffffff)](https://code.visualstudio.com/)
# nodejs typescript template
## Description
Template nodejs typescript
## Contributing
1. npm run start - Start project
2. npm run dev - Start project with ts-node (dev only)
3. npm run compile - Build ts
`)
// Generate index.ts
await fs.outputFile(`./packages/${packageName}/index.ts`, `// Hello
export function hello (name: string): string {
return 'hello ' + name
}
console.log(hello(' you'))
`)
// Generate index.test.ts
await fs.outputFile(`./packages/${packageName}/index.test.ts`, `import { describe, expect, test } from '@jest/globals'
import { hello } from './index'
describe('[index/hello] hello()', () => {
test('return "hello shopopop" when hello("you")', () => {
expect(hello('you')).toEqual('hello you')
})
})
`)
// Generate package.json
const pkg = require(`./packages/${packageName}/package.json`)
pkg.types = 'dist/index.d.ts'
pkg.main = 'dist/index.js'
pkg.scripts = {
start: 'node dist/index.js',
dev: 'ts-node index.ts',
compile: 'rimraf dist/* && tsc -p tsconfig.pkg.json'
}
pkg.description = ''
await fs.writeJSON(`./packages/${packageName}/package.json`, pkg, { spaces: 2 })
}
main()
.then(() => process.exit())
.catch((error) => {
console.warn(error)
process.exitCode(1)
})