1
1
mirror of https://github.com/primer/css.git synced 2024-12-14 23:12:03 +03:00
css/modules/generator-primer-module/app/index.js

160 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-09-01 08:23:17 +03:00
const chalk = require("chalk")
2017-09-01 21:57:00 +03:00
const fse = require("fs-extra")
2017-09-01 08:23:17 +03:00
const path = require("path")
const Generator = require("yeoman-generator")
const stripPrimerPrefix = require("./lib/strip-prefix")
const OPTIONS = require("./options")
2017-09-01 08:23:17 +03:00
module.exports = class PrimerModule extends Generator {
constructor(args, opts) {
super(args, opts)
2017-09-01 08:23:17 +03:00
2017-09-07 21:00:10 +03:00
// initialize positional arguments and option flags
Object.entries(OPTIONS).forEach(([name, val]) => {
if (val.argument) {
this.argument(name, Object.assign(val.argument, {name}))
} else if (val.option) {
2017-09-07 21:00:10 +03:00
const option = Object.assign(val.option, {name})
if (name.includes("_")) {
const alias = name.replace(/_/g, "-")
option.alias = option.alias
? [alias].concat(option.alias)
: alias
}
this.option(name, option)
}
2017-09-01 08:23:17 +03:00
})
}
initializing() {
this.log("It looks like you are adding a new Primer module!")
}
2017-09-01 08:23:17 +03:00
prompting() {
if (this.options.module) {
this.log("Great, let's get you started with %s...", this.options.module)
}
// filter out options without prompts, and which already
// have options set, then add back the "name" key to each
const prompts = Object.entries(OPTIONS)
.filter(([name, {prompt}]) => {
return prompt && !(name in this.options)
})
.map(([name, {prompt}]) => {
return Object.assign(prompt, {name})
})
2017-09-01 08:23:17 +03:00
// remove prompts for which arguments were already provided
return this.prompt(prompts)
2017-09-01 08:23:17 +03:00
.then(answers => {
Object.assign(this.options, answers)
})
}
configuring() {
this.dependencies = this._getDependencies()
2017-09-01 21:57:00 +03:00
if (this.options.docs) {
return fse.readFile(this.options.docs, "utf8")
.then(docs => this.options.docs = docs)
}
2017-09-01 08:23:17 +03:00
}
paths() {
this.basePath = this.destinationPath(this.options.module)
}
writing() {
this.log("creating: %s", chalk.green(this.basePath))
const data = {
"dependencies": this.dependencies,
}
Object.assign(
data,
Object.entries(OPTIONS)
.map(([name, value]) => [name, this.options[name]])
.reduce((acc, [name, value]) => {
acc[name] = value
return acc
}, {})
)
if (this.options.verbose) {
const debugData = Object.assign({}, data, {
"dependencies": Object.keys(data.dependencies),
})
console.warn(chalk.green("data:"), JSON.stringify(debugData, null, " "))
}
2017-09-01 08:23:17 +03:00
// for the index.scss import
data.lib = stripPrimerPrefix(data.module)
2017-09-01 08:23:17 +03:00
// copy the whole directory with each file treated as
// an EJS template
this.fs.copyTpl(
this.templatePath(),
this.basePath,
data
)
2017-09-01 21:57:00 +03:00
const src = path.join(this.basePath, "lib/module.scss")
const dest = src.replace("module.scss", `${data.lib}.scss`)
2017-09-01 21:57:00 +03:00
this.fs.move(src, dest)
2017-09-01 08:23:17 +03:00
}
install() {
const pkg = this.fs.readJSON(
path.join(this.basePath, "package.json")
)
// this.log("package:", pkg.name, "@", pkg.version)
if (Array.isArray(this.options.dependents)) {
this.options.dependents.forEach(dependent => {
this._addAsDependencyTo(pkg, dependent)
})
}
2017-09-01 08:23:17 +03:00
}
end() {
if (this.options.todo === true) {
this.log(
chalk.yellow("Remember to fill in any remaining TODOs below:")
)
this.spawnCommandSync("ack", ["TODO", this.basePath], {
stdio: "inherit",
})
}
2017-09-01 08:23:17 +03:00
}
_getDependencies() {
return [
"primer-support",
].reduce((deps, module) => {
deps[module] = require(`${module}/package.json`)
return deps
}, {})
}
_addAsDependencyTo(pkg, dest) {
this.log(
"adding %s@%s as a dependency to %s...",
2017-09-07 02:36:14 +03:00
pkg.name, pkg.version, dest
2017-09-01 08:23:17 +03:00
)
const destPath = require.resolve(
2017-09-07 02:58:30 +03:00
path.join("../../", dest, "package.json")
2017-09-01 08:23:17 +03:00
)
this.fs.extendJSON(destPath, {
dependencies: {
[pkg.name]: pkg.version,
},
})
}
}