-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathfeature-init.ts
More file actions
101 lines (91 loc) · 2.41 KB
/
Copy pathfeature-init.ts
File metadata and controls
101 lines (91 loc) · 2.41 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
// This script helps you start writing a new feature YAML file.
// To get started, run: `npm run --silent feature-init -- --help`
import fs from "node:fs/promises";
import { fileURLToPath } from "url";
import * as prettier from "prettier";
import { stringify } from "yaml";
import yargs from "yargs";
const argv = yargs(process.argv.slice(2))
.scriptName("feature-init")
.usage("$0 <feature-identifier>", "Start a new feature YAML file")
.positional("feature-identifier", {
describe: "the feature key (i.e., the filename without `.yml`)",
type: "string",
})
.option("dry-run", {
alias: "n",
boolean: true,
default: false,
describe: "Print instead of writing to a file",
})
.option("name", {
type: "string",
demandOption: true,
default: "",
describe: "A human-readable name",
})
.option("description", {
alias: "desc",
type: "string",
demandOption: true,
default: "",
describe: "A short description",
})
.option("caniuse", {
type: "string",
demandOption: true,
default: "",
describe: "Set the Can I use…? ID",
})
.option("compat-features", {
alias: "compat",
type: "array",
demandOption: true,
default: [],
describe: "A BCD key. Can be used multiple times.",
})
.option("spec", {
demandOption: true,
default: "",
describe: "A specification URL. Can be used multiple times.",
})
.parseSync();
async function main() {
const {
dryRun,
featureIdentifier,
name,
description,
caniuse,
compatFeatures,
spec,
} = argv;
const destination = identifierToPath(featureIdentifier);
const content = {
name,
description,
spec,
caniuse,
compat_features: compatFeatures,
};
const yamlText = stringify(content);
const formatted = await format(destination, yamlText);
if (dryRun) {
console.log(formatted);
process.exit(0);
}
await fs.writeFile(destination, formatted);
console.log(destination);
}
async function format(featurePath: string, text: string): Promise<string> {
const configPath = fileURLToPath(new URL("../.prettierrc", import.meta.url));
const options = await prettier.resolveConfig(configPath);
options.filepath = featurePath;
return prettier.format(text, options);
}
function identifierToPath(identifier: string): string {
return fileURLToPath(
new URL(`../features/${identifier}.yml`, import.meta.url),
);
}
await main();