-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcreate.js
More file actions
150 lines (129 loc) · 3.77 KB
/
create.js
File metadata and controls
150 lines (129 loc) · 3.77 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const inquirer = require('inquirer')
const path = require('node:path')
const createPackageJson = require('create-package-json')
const fs = require('fs-extra')
const { gitInit } = require('../git.js')
async function create (opts) {
const { directory } = opts
const config = {
path: directory,
name: undefined,
labels: [],
orgs: [],
projects: [],
githubActions: false
}
const answers = await inquirer.prompt([
{
type: 'input',
name: 'path',
message: 'Where would you like to create your project?',
default: 'statusboard',
when: () => !config.path
},
{
type: 'confirm',
name: 'defaultLabels',
message: 'Do you want use default labels?',
default: true
},
{
type: 'input',
name: 'labels',
message: 'What labels do you want to use (use commas to separate)',
when: (answers) => !answers.defaultLabels
},
{
type: 'input',
name: 'orgs',
message:
'What organizations do you want to include (use commas to separate)'
},
{
type: 'input',
name: 'repositories',
message:
'What repositories do you want to include (use commas to separate)'
},
{
type: 'confirm',
name: 'githubActions',
message: 'Do you want deploy with GitHub Actions?',
default: false
}
])
config.path = answers.path.trim()
config.labels = transformUserInput(answers.labels)
config.orgs = transformUserInput(answers.orgs)
config.projects = transformUserInput(answers.repositories)
config.githubActions = answers.githubActions
const appPath = path.resolve(config.path)
config.name = path.basename(appPath)
config.path = appPath
await fs.ensureDir(appPath)
if (config.githubActions) {
createBuildAction(config)
}
await createConfigFile(config)
createGitIgnore(config)
await createPackageJson({
cwd: appPath,
name: config.name,
description: 'A dashboard for project status',
version: '1.0.0',
dependencies: ['@pkgjs/statusboard'],
author: null,
keywords: ['statusboard', 'projects'],
repository: null,
type: 'commonjs',
private: true,
license: 'MIT',
main: 'config.js',
devDependencies: null,
scripts: {
build: 'statusboard build -C ./config.js',
buildsite: 'npm run clean && statusboard site -C ./config.js',
buildindex: 'statusboard index -C ./config.js',
clean: 'rm -rf build/css build/js'
}
})
gitInit(appPath)
}
async function createConfigFile (config) {
const configFile = path.join(config.path, 'config.js')
const configJs = `module.exports = {${
config.labels.length >= 1
? `\n issueLabels: ${JSON.stringify(config.labels)},`
: ''
}${config.orgs.length >= 1 ? `\n orgs: ${JSON.stringify(config.orgs)},` : ''}${
config.projects.length >= 1
? `\n projects: ${JSON.stringify(config.projects)},`
: ''
}${config.githubActions ? `\n baseUrl: "/${config.name}",` : ''}
github: {
token: process.env.GITHUB_TOKEN
}
}
`
await fs.writeFile(configFile, configJs)
}
function transformUserInput (input) {
if (input?.length === 0) {
return []
}
return input?.split(',').map((l) => l.trim()) || []
}
function createBuildAction (config) {
const workflowDir = path.join(config.path, '.github', 'workflows')
const templateFile = path.join(__dirname, 'template', 'build.yml')
const configFile = path.join(workflowDir, 'build.yml')
fs.ensureDirSync(workflowDir)
fs.copyFileSync(templateFile, configFile)
}
function createGitIgnore (config) {
const gitIgnore = path.join(config.path, '.gitignore')
const templateFile = path.join(__dirname, 'template', '.gitignore')
fs.ensureFileSync(gitIgnore)
fs.copyFileSync(templateFile, gitIgnore)
}
module.exports = create