Skip to content

Commit

Permalink
Install @statickit/deploy and refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Oct 18, 2019
1 parent 0e5e6d4 commit c7c4855
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules
.env
tmp
10 changes: 9 additions & 1 deletion 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 @@ -39,7 +39,7 @@
]
},
"dependencies": {
"axios": "^0.19.0",
"@statickit/deploy": "^0.3.0",
"chalk": "^2.4.2",
"dotenv": "^8.2.0",
"ora": "^4.0.2",
Expand Down
61 changes: 14 additions & 47 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
const fs = require('fs');
const chalk = require('chalk');
const axios = require('axios');
const deploy = require('@statickit/deploy');
const ora = require('ora');
const version = require('../../package.json').version;

const readConfigFromFile = file => {
try {
return fs.readFileSync(file, 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
return null;
} else {
throw err;
}
}
};

const parseConfig = rawConfig => {
try {
const json = JSON.parse(rawConfig);
return json;
} catch (err) {
return null;
}
};

const getConfig = args => {
if (args.config) return args.config;
return readConfigFromFile(args.file);
};

const getDeployKey = args => {
if (args.key) return args.key;
return process.env.STATICKIT_DEPLOY_KEY;
};

exports.command = ['deploy', '$0'];

exports.describe = 'Performs a deployment';
Expand Down Expand Up @@ -63,8 +31,9 @@ exports.builder = yargs => {
};

exports.handler = async args => {
const rawConfig = getConfig(args);
const rawConfig = deploy.getRawConfig(args);
const endpoint = args.endpoint || 'https://api.statickit.com';
const userAgent = `@statickit/cli@${version}`;
const spinner = ora(chalk.gray('Deploying...'));

if (!rawConfig) {
Expand All @@ -73,33 +42,31 @@ exports.handler = async args => {
return;
}

const config = parseConfig(rawConfig);
let config;

if (!config) {
try {
config = JSON.parse(rawConfig);
} catch (err) {
console.error(chalk.bold.red('Configuration could not be parsed'));
process.exitCode = 1;
return;
}

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

if (!deployKey) {
if (!key) {
console.error(chalk.bold.red('Deploy key not found'));
return;
}

spinner.start();

try {
const response = await axios({
method: 'post',
url: `${endpoint}/cli/v1/deployments`,
data: config,
headers: {
'StaticKit-Deploy-Key': deployKey,
'User-Agent': `@statickit/cli@${version}`
},
validateStatus: status => status < 500
const response = await deploy.request({
endpoint,
config,
key,
userAgent
});

spinner.stop();
Expand Down
104 changes: 50 additions & 54 deletions test/deploy.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
const execa = require('execa');
const fs = require('fs').promises;
const path = require('path');
require('dotenv').config();

const validMinimumConfig = `{"site":{"name":"Test Suite"}}`;
beforeEach(async () => {
// Create a temp working directory, since some of the tests
// need to create .env files and config files that might
// conflict with what's in the root of this repo.
await fs.mkdir('tmp');
});

afterEach(async () => {
const files = await fs.readdir('tmp');

// Delete all the files in the temp directory
for (const file of files) {
await fs.unlink(resolve(file));
}

await fs.rmdir('tmp');
});

const command = async (args, opts) => {
return await execa.command(
`../bin/statickit ${args}`,
Object.assign({ cwd: 'tmp' }, opts)
);
};

const resolve = file => {
return path.resolve('tmp', file);
};

it('returns help output', async () => {
const { stdout } = await execa('bin/statickit', ['--help']);
const { stdout } = await command('--help');
expect(stdout).toMatch(/Performs a deployment/);
});

it('returns an error if no config is present', async () => {
try {
await execa('bin/statickit', ['deploy']);
await command('deploy');
} catch (result) {
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Configuration not provided/);
Expand All @@ -20,7 +48,7 @@ it('returns an error if no config is present', async () => {

it('returns an error if config is unparsable', async () => {
try {
await execa('bin/statickit', ['deploy', '-c', "'{'"]);
await command(`deploy -c '{'`);
} catch (result) {
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Configuration could not be parsed/);
Expand All @@ -29,7 +57,7 @@ it('returns an error if config is unparsable', async () => {

it('returns an error if deploy key is not found', async () => {
try {
await execa('bin/statickit', ['deploy', '-c', "'{}'"]);
await command(`deploy -c '{}'`);
} catch (result) {
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Deploy key not found/);
Expand All @@ -38,7 +66,7 @@ it('returns an error if deploy key is not found', async () => {

it('returns an error if deploy key is invalid', async () => {
try {
await execa('bin/statickit', ['deploy', '-c', "'{}'", '-k', 'invalidkey']);
await command(`deploy -c '{}' -k invalidkey`);
} catch (result) {
console.log(result);
expect(result.exitCode).toBe(1);
Expand All @@ -47,76 +75,44 @@ it('returns an error if deploy key is invalid', async () => {
});

it('succeeds given valid params', async () => {
const { stdout } = await execa('bin/statickit', [
'deploy',
'-c',
validMinimumConfig,
'-k',
process.env.STATICKIT_TEST_DEPLOY_KEY
]);
const { stdout } = await command(
`deploy -c '{}' -k ${process.env.STATICKIT_TEST_DEPLOY_KEY}`
);
expect(stdout).toMatch(/Deployment succeeded/);
});

it('accepts a deploy key from env', async () => {
const { stdout } = await execa(
'bin/statickit',
['deploy', '-c', validMinimumConfig],
{
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
}
);
const { stdout } = await command(`deploy -c '{}'`, {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
expect(stdout).toMatch(/Deployment succeeded/);
});

it('accepts a deploy key from .env file', async () => {
let originalDotenv;

try {
originalDotenv = await fs.readFile('.env', 'utf8');
} catch (e) {
originalDotenv = '';
}

await fs.writeFile(
'.env',
`${originalDotenv}\nSTATICKIT_DEPLOY_KEY=${process.env.STATICKIT_TEST_DEPLOY_KEY}`,
resolve('.env'),
`STATICKIT_DEPLOY_KEY=${process.env.STATICKIT_TEST_DEPLOY_KEY}`,
'utf8'
);

const { stdout } = await execa('bin/statickit', [
'deploy',
'-c',
validMinimumConfig
]);
const { stdout } = await command(`deploy -c '{}'`);
expect(stdout).toMatch(/Deployment succeeded/);

if (originalDotenv !== '') {
await fs.writeFile('.env', originalDotenv, 'utf8');
}
});

it('accepts a config from the statickit.json file', async () => {
await fs.writeFile('statickit.json', validMinimumConfig, 'utf8');
await fs.writeFile(resolve('statickit.json'), '{}', 'utf8');

const { stdout } = await execa('bin/statickit', ['deploy'], {
const { stdout } = await command('deploy', {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
expect(stdout).toMatch(/Deployment succeeded/);

await fs.unlink('statickit.json');
});

it('accepts a config from a custom file', async () => {
await fs.writeFile('statickit-custom.json', validMinimumConfig, 'utf8');

const { stdout } = await execa(
'bin/statickit',
['deploy', '-A', 'statickit-custom.json'],
{
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
}
);
expect(stdout).toMatch(/Deployment succeeded/);
await fs.writeFile(resolve('statickit-custom.json'), '{}', 'utf8');

await fs.unlink('statickit-custom.json');
const { stdout } = await command('deploy -A statickit-custom.json', {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
expect(stdout).toMatch(/Deployment succeeded/);
});

0 comments on commit c7c4855

Please sign in to comment.