-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsaofile.js
More file actions
136 lines (132 loc) · 3.95 KB
/
saofile.js
File metadata and controls
136 lines (132 loc) · 3.95 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
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
'use strict'
const validateNpmPackageName = require('validate-npm-package-name')
const SUPPORTED_NPM_CLIENTS = ['pnpm', 'npm']
module.exports = {
description: 'Scaffolding out a node library.',
templateData: {
year: new Date().getFullYear(),
npmClientInstall: ({ npmClient }) => {
return npmClient === 'pnpm' ? 'add' : 'install'
},
changesetRepo: ({ projectRepository }) => {
// Extract owner/repo from URL like https://github.com/username/reponame
// or https://github.com/username/reponame.git
if (!projectRepository || typeof projectRepository !== 'string') return ''
const match = projectRepository.match(/github\.com\/([^/]+\/[^/.?#]+)/)
return match ? match[1] : ''
}
},
prompts() {
return [
{
name: 'npmClient',
message: 'Which package manager do you want to use?',
default: 'pnpm',
type: 'list',
choices: SUPPORTED_NPM_CLIENTS
},
{
name: 'projectName',
message: 'What is the name of the new project',
default: this.outFolder,
filter: val => val.toLowerCase(),
validate: projectName => {
const validation = validateNpmPackageName(projectName)
return !validation.errors && !validation.warnings
}
},
{
name: 'description',
message: 'How would you describe the new project',
default: ''
},
{
name: 'keywords',
message: 'Comma-separated list of package keywords for npm',
default: ''
},
{
name: 'author',
message: 'What is your name',
default: this.gitUser.name,
store: true,
required: true
},
{
name: 'username',
message: 'What is your GitHub username',
default:
this.gitUser.username ||
this.gitUser.name
.toLowerCase()
.split(' ')
.join(''),
filter: val => val.toLowerCase(),
store: true
},
{
name: 'email',
message: 'What is your email?',
default: this.gitUser.email,
store: true,
validate: v => /.+@.+/.test(v)
},
{
name: 'projectRepository',
message: 'The URL of the repository',
default({ username, projectName }) {
return `https://github.com/${username}/${projectName}`
},
store: true
}
]
},
actions() {
const npmClient = this.answers.npmClient
const lockfile = npmClient === 'pnpm' ? 'pnpm-lock.yaml' : 'package-lock.json'
return [
{
type: 'add',
templateDir: 'template',
files: '**'
},
{
type: 'modify',
files: 'package.json',
handler(data, filepath) {
data.scripts[
'lint:lockfile'
] = `lockfile-lint --path ${lockfile} --validate-https --allowed-hosts npm`
data.scripts['lint'] = `eslint . && ${npmClient} run lint:lockfile && ${npmClient} run lint:markdown`
data['lint-staged'] = {
'**/*.{js,json}': [`${npmClient} run lint:fix`]
}
if (npmClient !== 'pnpm') {
delete data.packageManager
}
return data
}
},
{
type: 'move',
patterns: {
gitignore: '.gitignore',
npmrc: '.npmrc'
}
}
]
},
async completed() {
const spawn = require('child_process').spawnSync
spawn('git', ['init', '-b', 'main'], { cwd: this.outDir })
const repoUrl = this.answers.projectRepository
if (repoUrl) {
const remoteUrl = repoUrl.endsWith('.git') ? repoUrl : `${repoUrl}.git`
spawn('git', ['remote', 'add', 'origin', remoteUrl], { cwd: this.outDir })
}
await this.npmInstall({ npmClient: this.answers.npmClient })
await this.npmInstall({ npmClient: this.answers.npmClient, args: ['run', 'prepare'] })
this.showProjectTips()
this.logger.tip('You\'re all setup. hack away!')
}
}