1
0
Fork 0
freesewing/packages/create-freesewing-pattern/lib/create-library.js

150 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict'
2019-05-09 15:50:59 +02:00
const handlebars = require('handlebars')
const execa = require('execa')
const fs = require('fs')
const globby = require('globby')
const normalize = require('normalize-path')
const mkdirp = require('make-dir')
const ora = require('ora')
const path = require('path')
const pEachSeries = require('p-each-series')
2019-05-09 15:50:59 +02:00
const pkg = require('../package')
2019-05-09 15:50:59 +02:00
const templateBlacklist = new Set([path.join('example', 'public', 'favicon.ico')])
2019-05-09 15:50:59 +02:00
module.exports = async (info) => {
const { manager, template, name, templatePath, git } = info
2018-03-04 15:43:18 -05:00
2018-03-09 18:45:50 -05:00
// handle scoped package names
const parts = name.split('/')
info.shortName = parts[parts.length - 1]
2018-03-09 18:45:50 -05:00
const dest = path.join(process.cwd(), info.shortName)
info.dest = dest
await mkdirp(dest)
2018-03-04 15:43:18 -05:00
2019-05-09 15:50:59 +02:00
const source =
template === 'custom'
2019-05-09 15:50:59 +02:00
? path.join(process.cwd(), templatePath)
: path.join(__dirname, '..', 'template', template)
const files = await globby(normalize(source), {
2018-03-04 15:43:18 -05:00
dot: true
})
2018-03-04 15:43:18 -05:00
{
const promise = pEachSeries(files, async (file) => {
2018-03-04 15:43:18 -05:00
return module.exports.copyTemplateFile({
file,
source,
dest,
info
})
})
ora.promise(promise, `Copying ${template} template to ${dest}`)
await promise
2018-03-04 15:43:18 -05:00
}
{
const promise = module.exports.initPackageManager({ dest, info })
ora.promise(promise, `Running ${manager} install and ${manager} link`)
await promise
2018-03-04 15:43:18 -05:00
}
if (git) {
const promise = module.exports.initGitRepo({ dest })
ora.promise(promise, 'Initializing git repo')
await promise
2018-03-04 15:43:18 -05:00
}
2018-03-09 18:45:50 -05:00
return dest
}
2018-03-04 15:43:18 -05:00
module.exports.copyTemplateFile = async (opts) => {
const { file, source, dest, info } = opts
2018-03-04 15:43:18 -05:00
const fileRelativePath = path.relative(source, file)
const destFilePath = path.join(dest, fileRelativePath)
const destFileDir = path.parse(destFilePath).dir
2018-03-04 15:43:18 -05:00
await mkdirp(destFileDir)
2019-03-03 10:13:05 -05:00
if (templateBlacklist.has(fileRelativePath)) {
const content = fs.readFileSync(file)
fs.writeFileSync(destFilePath, content)
2019-03-03 10:13:05 -05:00
} else {
const template = handlebars.compile(fs.readFileSync(file, 'utf8'))
2019-03-03 10:13:05 -05:00
const content = template({
...info,
yarn: info.manager === 'yarn'
})
2019-03-03 10:13:05 -05:00
fs.writeFileSync(destFilePath, content, 'utf8')
2019-03-03 10:13:05 -05:00
}
2018-03-04 15:43:18 -05:00
return fileRelativePath
}
2018-03-04 15:43:18 -05:00
module.exports.initPackageManager = async (opts) => {
const { dest, info } = opts
2018-03-04 15:43:18 -05:00
const example = path.join(dest, 'example')
2018-05-11 18:32:26 -04:00
2018-03-04 15:43:18 -05:00
const commands = [
{
2018-03-06 22:42:54 -05:00
cmd: `${info.manager} install`,
2018-03-04 15:43:18 -05:00
cwd: dest
},
{
cmd: `${info.manager} link`,
cwd: dest
},
2018-05-11 18:32:26 -04:00
{
cmd: `${info.manager} install`,
2018-05-11 18:32:26 -04:00
cwd: example
}
]
2018-03-04 15:43:18 -05:00
return pEachSeries(commands, async ({ cmd, cwd }) => {
return execa.sync(cmd, { cwd, shell: true })
})
}
2018-03-04 15:43:18 -05:00
module.exports.initGitRepo = async (opts) => {
const { dest } = opts
2018-03-04 15:43:18 -05:00
const gitIgnorePath = path.join(dest, '.gitignore')
2019-05-09 15:50:59 +02:00
fs.writeFileSync(
gitIgnorePath,
`
2018-03-04 19:44:19 -05:00
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
node_modules
# builds
build
dist
2018-10-01 13:39:09 -04:00
.rpt2_cache
2018-03-04 19:44:19 -05:00
# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
2019-05-09 15:50:59 +02:00
`,
'utf8'
)
const cmd = `git init && git add . && git commit -m ":tada: Initialized ${pkg.name}@${pkg.version} with create-freesewing-pattern"`
return execa.sync(cmd, { cwd: dest, shell: true })
}