-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts-node.ts
177 lines (146 loc) · 3.34 KB
/
ts-node.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import {
VDir,
VFile,
VIgnoreFile,
VPackageJson,
npmPlugin,
PatchStrategy,
presetify,
} from 'puggle'
import dedent = require('dedent')
import {
addDocker,
addJestWithTypescript,
addPrettier,
addTypescript,
readResource,
readVFile,
useCli,
} from './utils/'
const indexTs = (name: string) => dedent`
//
// The app entrypoint
//
;(async () => {
console.log('Hello, ${name}!')
})()
`
const indexSpecTs = () => dedent`
//
// An example unit test
//
describe('sample', () => {
it('should pass', () => {
expect(1 + 1).toBe(2)
})
})
`
const toIgnore = ['*.env', '.DS_Store', 'node_modules', 'coverage', 'dist']
async function generateReadme(projectName: string) {
const dev = await Promise.all([
readResource('docs/setup.md'),
readResource('docs/regular-use.md'),
readResource('docs/testing.md'),
readResource('docs/irregular-use.md'),
readResource('docs/prettier.md'),
readResource('docs/release.md'),
])
return dedent`
# ${projectName}
## Usage
> Work in progress
## Development
${dev.join('\n')}
---
> This project was set up by [puggle](https://npm.im/puggle)
`
}
export default presetify({
name: 'robb-j:ts-node',
version: '0.3.1',
plugins: [npmPlugin],
async apply(root, { targetName, askQuestions }) {
let npm = VPackageJson.getOrFail(root)
//
// Setup testing
//
await addJestWithTypescript(root, npm)
//
// Setup typescript
//
await addTypescript(root, npm)
//
// Setup prettier
//
await addPrettier(root, npm, 'js,json,css,md,ts,tsx')
//
// Setup git
//
root.addChild(
new VIgnoreFile(
'.gitignore',
'Files to ignore from git source control',
toIgnore,
PatchStrategy.persist
)
)
//
// Setup editorconfig
//
root.addChild(
await readVFile('.editorconfig', '.editorconfig', PatchStrategy.persist)
)
//
// Setup template
//
await npm.addLatestDependencies({
debug: '4.x',
})
await npm.addLatestDevDependencies({
'@types/debug': '4.x',
dotenv: '10.x',
})
npm.addPatch('main', PatchStrategy.placeholder, 'dist/index.js')
npm.addPatch('types', PatchStrategy.placeholder, 'dist/index.d.js')
npm.addPatch('scripts', PatchStrategy.placeholder, {
preversion: 'npm run test && npm run build',
postversion: 'git push --follow-tags',
})
root.addChild(
new VFile('README.md', await generateReadme(targetName)),
new VFile('.env', 'NODE_ENV=development'),
new VDir('src', [
new VFile('index.ts', indexTs(targetName)),
new VDir('__test__', [new VFile('index.spec.ts', indexSpecTs())]),
])
)
//
// Setup docker
//
const docker = await askQuestions('docker', [
{
type: 'confirm',
name: 'enabled',
message: 'Use Docker?',
initial: false,
},
])
if (docker.enabled) {
await addDocker(root, docker, toIgnore)
}
//
// Setup CLI
//
const cli = await askQuestions('cli', [
{
type: 'confirm',
name: 'enabled',
message: 'Create a CLI app?',
initial: false,
},
])
if (cli.enabled) {
await useCli(root, npm, targetName, docker.enabled, true)
}
},
})