-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·164 lines (142 loc) · 4.36 KB
/
index.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
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
#!/usr/bin/env node
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const { promisify } = require('util');
const sade = require('sade');
const mkdir = require('make-dir');
const prompts = require('prompts');
const semver = require('semver');
const write = require('write-json-file');
const ejs = require('ejs');
const sort = require('sort-package-json');
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const prog = sade('init-pkg <project_user> [name]');
const readTemplate = async (name, data = {}) => {
const file = (await readFile(path.join(__dirname, 'templates', `${name}.ejs`))).toString();
return ejs.render(file, data);
};
prog
.version(require('./package').version)
.option('-t, --typescript', 'Initialize the package with typescript set up')
.option('-b, --bitbucket', 'Initialize the repo with pointing at Bitbucket instead of GitHub')
.option('--exclude-ava', 'Exclude Ava installation and configuration')
.option('--exclude-prettier', `Exclude Renddslow's opinionated prettier setup`)
.action(async (user, name, opts) => {
if (!name && !user.includes('/')) throw new Error('');
if (user.includes('/')) {
[user, name] = user.split('/');
}
await mkdir(name);
const dir = path.join(process.cwd(), name);
const repository = opts.b ? `https://bitbucket.org/${user}/${name}` : `https://github.com/${user}/${name}`;
execSync('git init', {
cwd: dir,
});
execSync(`git remote add origin ${repository}`, {
cwd: dir,
});
await Promise.all([
writeFile(path.join(dir, '.gitignore'), await readTemplate('gitignore')),
writeFile(path.join(dir, 'yarn.lock'), ''),
]);
const pkg = await prompts([
{
name: 'version',
type: 'text',
message: 'version',
initial: '1.0.0',
validate: (v) => semver.valid(semver.coerce(v)) ? true : 'Must be a valid semver version',
format: (v) => semver.coerce(v).version,
},
{
name: 'description',
type: 'text',
initial: '',
message: 'description',
},
{
name: 'main',
type: 'text',
initial: 'index.js',
message: 'entry point',
},
{
name: 'license',
type: 'text',
initial: 'MIT',
message: 'license'
},
{
name: 'author',
type: 'text',
initial: '',
message: 'author'
},
]);
pkg.name = name;
pkg.private = false;
pkg.repository = repository;
pkg.scripts = {};
const devDependencies = [];
if (!opts['exclude-prettier']) {
pkg.prettier = '@dmsi/prettier-config';
pkg.husky = {
hooks: {
'pre-commit': 'lint-staged',
},
};
pkg['lint-staged'] = {
'*.{js,css,json,md,ts,tsx}': [
'prettier --write',
]
};
devDependencies.push(...[
'husky',
'prettier',
'lint-staged',
'@dmsi/prettier-config',
]);
}
if (!opts['exclude-ava']) {
pkg.ava = {
files: ['src/**/*.test.ts'],
concurrency: 4,
timeout: '1m',
babel: false,
compileEnhancements: false,
extensions: ['ts'],
require: ['ts-node/register']
};
devDependencies.push('ava');
}
await write(path.join(dir, 'package.json'), sort(pkg));
const readme = await readTemplate('readme', {
name,
description: pkg.description,
});
await writeFile(path.join(dir, 'README.md'), readme);
execSync('git add -A', { cwd: dir });
execSync('git commit -m "[init-pkg] Initial commit"', { stdio: 'inherit', cwd: dir });
if (opts.typescript) {
devDependencies.push(
'@types/node',
'ts-node',
'typescript',
);
pkg.scripts.build = 'tsc';
await writeFile(
path.join(dir, 'tsconfig.json'),
await readFile(path.join(__dirname, 'templates', 'tsconfig.json')),
);
}
execSync(`yarn add --dev ${devDependencies.join(' ')}`, {
stdio: 'inherit',
cwd: dir,
});
execSync('git add -A', { cwd: dir });
execSync('git commit -m "[init-pkg] Added base dev dependencies"', { stdio: 'inherit', cwd: dir });
console.log(`🦄 ${name} has been created. Have fun storming the castle!`);
});
prog.parse(process.argv);