UNPKG

create-custom-skill

Version:

An Alexa custom skill for the Amazon Echo.

142 lines (129 loc) 4.71 kB
#!/usr/bin/env node 'use strict' const fs = require('fs-extra') const path = require('path') const shell = require('shelljs') const yaml = require('js-yaml') const changeCase = require('change-case') const inquirer = require('inquirer') const os = require('os') const program = require('commander') const chalk = require('chalk') const ora = require('ora') const { spawnSync } = require('child_process') program .version('0.0.14') .option( '-p, --profile [profile]', 'Choose your [profile] from ask-cli tool (~/.ask/cli_config)', 'default' ) .parse(process.argv) if (!program.args.length) { console.error('skill name is required (dash separated string)') process.exit(1) } const destinationDirectoryName = program.args[0] const handleExit = () => { process.exit() } const handleError = e => { console.error('ERROR! An error was encountered while executing') console.error(e) console.log('Exiting with error.') process.exit(1) } process.on('SIGINT', handleExit) process.on('uncaughtException', handleError) fs.readdirSync(__dirname) .filter(file => !['.git', 'node_modules', 'bin.js'].includes(file)) .forEach(file => { //modify package.json if (file === 'package.json') { const pkg = fs.readJSONSync(path.join(__dirname, file)) pkg.name = changeCase.paramCase(destinationDirectoryName) delete pkg.bin delete pkg.scripts['create-custom-skill'] pkg.description = 'An Alexa skill for the Amazon Echo.' delete pkg.dependencies['fs-extra'] delete pkg.dependencies['shelljs'] delete pkg.dependencies['change-case'] delete pkg.dependencies['js-yaml'] delete pkg.dependencies['inquirer'] delete pkg.dependencies['commander'] delete pkg.dependencies['ora'] delete pkg.shelljs pkg.version = '1.0.0' pkg.files = pkg.files.filter(file => file === 'src') const packageJson = Object.keys(pkg) .filter(setting => !setting.startsWith('_')) .filter( setting => !['bundleDependencies', 'deprecated'].includes(setting) ) .reduce((obj, key) => { obj[key] = pkg[key] return obj }, {}) fs.writeJSONSync(path.join(__dirname, file), packageJson) } if (file === '.npmignore') { try { fs.copyFileSync( path.join(__dirname, file), path.join(__dirname, '.gitignore') ) } catch (error) { console.log('No .npmignore file ', error) } } if (file === 'serverless.yml') { try { const sls = yaml.safeLoad( fs.readFileSync(path.join(__dirname, file), 'utf8') ) sls.service = changeCase.paramCase(destinationDirectoryName) sls.custom.alexa.skill.invocationName = changeCase.noCase( destinationDirectoryName ) sls.custom.alexa.profile = program.profile fs.writeFileSync( path.join(__dirname, file), yaml.safeDump(sls), 'utf8' ) } catch (error) { console.log('Unable to process serverless.yml file ', error) } } if (file === 'skill.json' || file === 'debugSkill.json') { const skill = fs.readJSONSync(path.join(__dirname, file)) const locales = skill.manifest.publishingInformation.locales skill.manifest.publishingInformation.locales = Object.keys( locales ).reduce((obj, curr) => { obj[curr] = locales[curr] obj[curr].name = changeCase.titleCase(destinationDirectoryName) return obj }, {}) fs.writeJSONSync(path.join(__dirname, file), skill) } fs.copySync( path.join(__dirname, file), path.join(destinationDirectoryName, file) ) }) const spinner = ora('Now running npm install...').start() const child = spawnSync( `npm --prefix ./${destinationDirectoryName} install ./${destinationDirectoryName}`, { stdio: 'inherit', shell: true, } ) if (child.status !== 0) { spinner.fail('Error installing packages.') } spinner.succeed( 'You can now cd to ' + destinationDirectoryName + ' and start coding.' )