Skip to content

Commit

Permalink
Implement init command
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Oct 25, 2019
1 parent 7c4855e commit 1688276
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 84 deletions.
9 changes: 1 addition & 8 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ const chalk = require('chalk');
const deploy = require('@statickit/deploy');
const ora = require('ora');
const version = require('../../package.json').version;
const { formatLine, formatError } = require('../output');

const setErrorExit = () => {
process.exitCode = 1;
};

const formatLine = str => {
return `${chalk.gray('>')} ${str}`;
};

const formatError = msg => {
return formatLine(chalk.red(msg));
};

exports.command = 'deploy';

exports.describe = 'Performs a deployment';
Expand Down
16 changes: 15 additions & 1 deletion src/cmds/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
const { formatLine } = require('../output');
const chalk = require('chalk');
const fs = require('fs');

const logSuccess = msg => {
console.log(formatLine(chalk.green(msg)));
};

exports.command = 'init';
exports.desc = 'Create a statickit.json file in the current directory';
exports.builder = {};
exports.handler = function(args) {
console.log('init called');
fs.writeFile('statickit.json', '{}', { flag: 'wx' }, function(err) {
if (err) {
logSuccess('statickit.json already exists');
} else {
logSuccess('statickit.json created');
}
});
};
11 changes: 11 additions & 0 deletions src/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const chalk = require('chalk');

const formatLine = str => {
return `${chalk.gray('>')} ${str}`;
};

const formatError = msg => {
return formatLine(chalk.red(msg));
};

module.exports = { formatLine, formatError };
51 changes: 0 additions & 51 deletions test/helpers.js

This file was deleted.

91 changes: 67 additions & 24 deletions test/deploy.test.js → test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,102 @@
const fs = require('fs').promises;

const {
command,
makeSandbox,
removeSandbox,
sandboxCommand,
resolveSandbox
} = require('./helpers');

describe('using file system', () => {
beforeEach(async () => {
await makeSandbox();
require('dotenv').config();

const fs = require('fs');
const execa = require('execa');
const path = require('path');

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

// Resolve path to file in the working directory for the command
const resolveSandbox = file => {
return path.resolve('tmp', file);
};

beforeEach(async () => {
// Create a sandbox directory to serve a the command working directory,
// since tests that interact with the file system conflict with files
// in the repo.
fs.mkdirSync('tmp');
});

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

// Delete all the files in the temp directory
for (const file of files) {
fs.unlinkSync(resolveSandbox(file));
}

fs.rmdirSync('tmp');
});

describe('init', () => {
it('creates an empty statickit.json file', async () => {
const path = resolveSandbox('statickit.json');

expect(() => {
fs.readFileSync(path, 'utf8');
}).toThrow(/no such file or directory/);

const { stdout } = await command('init');

expect(fs.readFileSync(path, 'utf8')).toMatch(/{}/);
expect(stdout).toMatch(/statickit\.json created/);
});

afterEach(async () => {
await removeSandbox();
it('does not overwrite existing files', async () => {
const path = resolveSandbox('statickit.json');
const contents = '{"forms":{}}';
fs.writeFileSync(path, contents);

const { stdout } = await command('init');

expect(fs.readFileSync(path, 'utf8')).toBe(contents);
expect(stdout).toMatch(/statickit\.json already exists/);
});
});

describe('deploy', () => {
it('returns an error if no config is present', async () => {
try {
await sandboxCommand('deploy');
await command('deploy');
} catch (result) {
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Configuration not provided/);
}
});

it('accepts a deploy key from .env file', async () => {
await fs.writeFile(
fs.writeFileSync(
resolveSandbox('.env'),
`STATICKIT_DEPLOY_KEY=${process.env.STATICKIT_TEST_DEPLOY_KEY}`,
'utf8'
);

const { stdout, exitCode } = await sandboxCommand(`deploy -c '{}'`);
const { stdout, exitCode } = await command(`deploy -c '{}'`);
expect(exitCode).toBe(0);
expect(stdout).toMatch(/Deployment succeeded/);
});

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

const { stdout, exitCode } = await sandboxCommand('deploy', {
const { stdout, exitCode } = await command('deploy', {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
expect(exitCode).toBe(0);
expect(stdout).toMatch(/Deployment succeeded/);
});

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

const { stdout, exitCode } = await sandboxCommand(
const { stdout, exitCode } = await command(
'deploy --file statickit-custom.json',
{
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
Expand All @@ -60,9 +105,7 @@ describe('using file system', () => {
expect(exitCode).toBe(0);
expect(stdout).toMatch(/Deployment succeeded/);
});
});

describe('using args', () => {
it('returns an error if config is unparsable', async () => {
try {
await command(`deploy -c '{'`);
Expand Down

0 comments on commit 1688276

Please sign in to comment.