Skip to content

Commit

Permalink
Auto-install the shim
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Jan 9, 2020
1 parent bf1aa60 commit f6a9fe1
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 169 deletions.
32 changes: 8 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"chalk": "^2.4.2",
"common-tags": "^1.8.0",
"dotenv": "^8.2.0",
"execa": "^2.0.4",
"ora": "^4.0.2",
"pacote": "^10.1.3",
"yargs": "^15.0.2"
},
"devDependencies": {
"execa": "^2.0.4",
"husky": "^3.0.9",
"jest": "^24.9.0",
"lint-staged": "^9.4.2",
Expand Down
30 changes: 28 additions & 2 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ora = require('ora');
const version = require('../../package.json').version;
const log = require('../log');
const messages = require('../messages');
const shim = require('../shim');
const { stripIndent } = require('common-tags');

const indent = (text, depth = 2) => {
Expand Down Expand Up @@ -147,10 +148,16 @@ exports.builder = yargs => {
describe: 'Path to the local `statickit.json` file',
default: 'statickit.json'
});

yargs.option('shim', {
describe: 'Install the functions shim package',
type: 'boolean',
default: true
});
};

exports.handler = async args => {
const rawConfig = deploy.getRawConfig(args);
const rawConfig = args.config || deploy.getRawConfig(args);
const endpoint = args.endpoint || 'https://api.statickit.com';
const userAgent = `@statickit/cli@${version}`;
const spinner = ora(chalk.gray('Deploying...'));
Expand All @@ -171,7 +178,7 @@ exports.handler = async args => {
return;
}

const key = deploy.getDeployKey(args);
const key = args.key || deploy.getDeployKey(args);

if (!key) {
messages.authRequired();
Expand All @@ -196,6 +203,25 @@ exports.handler = async args => {
log.success(
`Deployment succeeded ${chalk.gray(`(${response.data.id})`)}`
);

if (args.shim && response.data.shim) {
const shimSpinner = ora(chalk.gray('Installing functions...'));
shimSpinner.start();

try {
await shim.install(response.data.shim);
shimSpinner.stop();
log.success(
`Functions installed ${chalk.gray(`(${response.data.shim})`)}`
);
} catch (error) {
shimSpinner.stop();
log.error('Functions failed to install');
console.error(error);
process.exitCode = 1;
}
}

return;

case 401:
Expand Down
12 changes: 12 additions & 0 deletions src/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require('fs');
const execa = require('execa');

const install = async ref => {
if (fs.existsSync('yarn.lock')) {
await execa('yarn', ['add', ref]);
} else {
await execa('npm', ['install', ref]);
}
};

module.exports = { install };
38 changes: 38 additions & 0 deletions test/cmds/__snapshots__/deploy.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,44 @@ Array [
]
`;
exports[`does not install the shim if --no-shim is used 1`] = `
Array [
Array [
"✔ Deployment succeeded (xxxx-xxxx-xxxx)",
],
]
`;
exports[`does not install the shim if not present in the response 1`] = `
Array [
Array [
"✔ Deployment succeeded (xxxx-xxxx-xxxx)",
],
]
`;
exports[`installs the shim if present in the response 1`] = `
Array [
Array [
"✔ Deployment succeeded (xxxx-xxxx-xxxx)",
],
Array [
"✔ Functions installed (shim-ref)",
],
]
`;
exports[`outputs shim install errors 1`] = `
Array [
Array [
"✕ Functions failed to install",
],
Array [
[Error: error installing package],
],
]
`;
exports[`sends a deploy request with the right params 1`] = `
Array [
Array [
Expand Down
Loading

0 comments on commit f6a9fe1

Please sign in to comment.