-
Notifications
You must be signed in to change notification settings - Fork 13
/
create.js
172 lines (156 loc) · 6 KB
/
create.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
165
166
167
168
169
170
171
172
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const fs = require('fs');
const git = require('git-promise');
const { Command } = require('commander');
const { Confirm, Select } = require('enquirer');
const CANCEL = "(Cancel)";
const CREATION_PATH='content/en-US';
const API_PATH = `${CREATION_PATH}/api`;
const CSS_PATH = `${CREATION_PATH}/css`;
const TEMPLATE_PATH = "templates";
const TEMPLATES = {
"Interface": "interface.md",
"Constructor": "constructor.md",
"Event": "event.md",
"Method": "method.md",
"Property": "property.md",
"CSS": "css-property.md",
"Header": "header.md"
}
let branch;
const program = new Command();
program
.option('-r, --reuse-branch', 'Create a page using the existing git branch.')
.requiredOption('-i, --item-name <itemName>', 'The name of the item to create a page for.')
run();
async function run() {
printWelcome();
program.parse(process.argv);
const outputType = await getOutputType();
await resolveBranch();
makeBoilerplate(outputType, program.itemName);
}
function printWelcome() {
console.clear();
console.log("=".repeat(80));
console.log(" ".repeat(30) + "Welcome to Page Creator" + " ".repeat(29));
console.log("=".repeat(80));
console.log();
let welcomeMessage = `Thank you for helping document the web platform. What you will create is part\n`;
welcomeMessage += `off a reference site. It's more like a dictionary than a how-to guide. It\n`;
welcomeMessage += `requires consistency for the sake of being quickly and easily scanable and\n`;
welcomeMessage += `Please don't get creative. If you need to do something not covered by the\n`;
welcomeMessage += `provided instructions, please post a message to chrome-puppy-discuss@.`;
console.log(welcomeMessage);
console.log();
}
async function getOutputType() {
const prompt = new Select({
name: 'outputType',
message: 'What type of page do you want to create?',
choices: ["CSS", "Interface", "Constructor", "Event", "Method", "Property", CANCEL]
});
let answer = await prompt.run();
if (answer === CANCEL) { process.exit(); }
return answer;
}
async function resolveBranch() {
branch = await git("status -sb",
(stdout) => stdout.match(/## (.*)/)[1]);
if (branch.includes("puppy")) {
branch = program.itemName;
await git(`checkout -b ${branch}`);
console.log(`\nYour work is in a new branch called ${branch}.\n`);
return;
} else {
let reuse;
if (program.reuseBranch){
reuse = program.reuseBranch;
} else {
let prompt = `Do you want to use the ${branch} branch, created during the last run, for \n`;
prompt += ` ${program.itemName}? Use the '-r' flag to avoid this question in the future.`;
reuse = await confirm(prompt);
}
if (reuse) {
console.log(`\nWork will be in your existing '${branch}' branch.\n`);
} else {
branch = program.itemName;
git(`checkout -b ${branch} puppy`);
console.log(`\nWork will be in a new branch called '${branch}'.\n`);
}
}
}
async function confirm(msg, initial = "true") {
const prompt = new Confirm({
name: 'confirm',
message: msg,
initial: initial,
format: (v) => {
return v ? 'yes' : 'no';
}
});
return await prompt.run();
}
function makeBoilerplate(type, name) {
let outPath;
let templateName;
let msg;
switch (type) {
case 'CSS':
outPath = makeFolder(CSS_PATH);
templateName = TEMPLATES[type];
fs.copyFileSync(`${TEMPLATE_PATH}/${templateName}`, `${outPath}/${name}.md`);
msg = `Page Creator has created a boilerplate at ${outPath}/${name}.md.\n\n`;
break;
case 'Event':
outPath = makeFolder(API_PATH);
templateName = TEMPLATES[type];
fs.copyFileSync(`${TEMPLATE_PATH}/${templateName}`, `${outPath}/${name}.md`);
templateName = TEMPLATES['Method'];
fs.copyFileSync(`${TEMPLATE_PATH}/${templateName}`, `${outPath}/on${name}.md`);
msg = `Page Creator has created a boilerplate for the event at ${outPath}/${name}.md\n`
msg += `and a boilerplate or the event callback at ${outPath}/on${name}.md.\n`;
msg += `If your event callback takes an existing event type, delete ${outPath}/${name}.md/\n`;
msg += `For each file:\n\n`;
case 'Constructor':
outPath = makeFolder(API_PATH);
templateName = TEMPLATES[type];
fs.copyFileSync(`${TEMPLATE_PATH}/${templateName}`, `${outPath}/${name}${name}.md`);
msg = `Page Creator has created a boilerplate at ${outPath}/${name}${name}.md.\n\n`;
break;
default:
outPath = makeFolder(API_PATH);
templateName = TEMPLATES[type];
fs.copyFileSync(`${TEMPLATE_PATH}/${templateName}`, `${outPath}/${name}.md`);
msg = `Page Creator has created a boilerplate at ${outPath}/${name}.md.\n\n`;
break;
}
msg += `1. Open the file.\n`;
msg += `2. Replace square-bracketed [[tokens]] with the specified information.\n`;
msg += `3. Answer the questions in the file.\n`;
msg += `4. Commit all new files to the '${branch}' branch and push them to origin.\n`;
msg += ` Be sure not to include the package-lock.json.\n`
msg += `5. Open a browser and go to https://github.com/GoogleChromeLabs/stumptown-content.git\n`;
msg += `6. Open a pull request against the default branch.\n\n`;
msg += `Developer Relations will review your submission within a week and request\n`;
msg += `changes, if needed.\n`;
console.log(msg);
}
function makeFolder(dirName) {
if (fs.existsSync(dirName)) { return dirName; }
fs.mkdirSync(dirName);
return dirName;
}