@idearium/cli
Advanced tools
Comparing version 1.0.0-alpha.4 to 1.0.0-alpha.5
@@ -5,15 +5,12 @@ #!/usr/bin/env node | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const program = require('commander'); | ||
const handlebars = require('handlebars'); | ||
const dotenv = require('dotenv'); | ||
const { test } = require('shelljs'); | ||
const { executeTemplate, writeFile } = require('./lib/c'); | ||
// Set the default values that `<value>` can be. | ||
const values = ['reset']; | ||
// Setup the program. | ||
program | ||
.arguments('<value>') | ||
.description(`The <value> can be one of the following: ${values.toString()}.`) | ||
.description('The <value> can be either reset, or an environment to modify Docker Compose.') | ||
.parse(process.argv); | ||
@@ -24,69 +21,29 @@ | ||
// Make sure the value provided was one of the acceptable values. | ||
/* eslint-disable padded-blocks */ | ||
if (!values.includes(value)) { | ||
program.help(); | ||
} | ||
/* eslint-enable padded-blocks */ | ||
// Let's get to the root of the project. | ||
const root = path.resolve(__dirname, '..'); | ||
const projectRoot = path.resolve(process.cwd()); | ||
/** | ||
* Given a template file, and a locals object, compile the template and generate the result. | ||
* @param {String} file A path to a template file. | ||
* @param {Object} [locals={}] The locals to the pass to the template. | ||
* @return {Promise} A promise. | ||
*/ | ||
const executeTemplate = (file, locals = {}) => new Promise((resolve, reject) => { | ||
// Load the current configuration, so we don't loose any previous values. | ||
const locals = dotenv.config(); | ||
fs.readFile(path.join(root, 'templates', file), 'utf-8', (err, data) => { | ||
// Set the default value for `COMPOSE_FILE`. | ||
locals.COMPOSE_FILE = 'docker-compose.yml'; | ||
/* eslint-disable padded-blocks */ | ||
if (err) { | ||
return reject(err); | ||
} | ||
/* eslint-enable padded-blocks */ | ||
// Let's determine if the file exists first. | ||
if (value !== 'reset') { | ||
const template = handlebars.compile(data); | ||
const file = `docker-compose.${value}.yml`; | ||
return resolve(template(locals)); | ||
/* eslint-disable padded-blocks */ | ||
if (!test('-f', path.join(projectRoot, file))) { | ||
throw Error(`File ${file} did not exist.`); | ||
} | ||
/* eslint-enable padded-blocks */ | ||
}); | ||
// Update it with the second file. | ||
locals.COMPOSE_FILE += `:${file}`; | ||
}); | ||
/** | ||
* Give some content, write it to a file. | ||
* @param {String} content A string, representing the content to write to file. | ||
* @param {String} file A path to a file to write. | ||
* @return {Promise} A promise. | ||
*/ | ||
const writeFile = (content, file) => new Promise((resolve, reject) => { | ||
fs.writeFile(file, content, (err) => { | ||
/* eslint-disable padded-blocks */ | ||
if (err) { | ||
return reject(err); | ||
} | ||
/* eslint-enable padded-blocks */ | ||
return resolve(); | ||
}); | ||
}); | ||
// Load the current configuration, so we don't loose any previous values. | ||
const locals = dotenv.config(); | ||
/* eslint-disable padded-blocks */ | ||
if (value === 'reset') { | ||
locals.COMPOSE_FILE = 'docker-compose.yml'; | ||
} | ||
/* eslint-enable padded-blocks */ | ||
// Execute the template, then write out the new file. | ||
return executeTemplate('.env.tmpl', locals) | ||
.then(str => writeFile(str, path.join(root, '.env'))); | ||
.then(str => writeFile(path.join(projectRoot, '.env'), str)); |
'use strict'; | ||
const { readFile } = require('fs'); | ||
const { readFile, writeFile } = require('fs'); | ||
const { homedir } = require('os'); | ||
const { join } = require('path'); | ||
const { compile } = require('handlebars'); | ||
const { promisify } = require('util'); | ||
/** | ||
* Given a template file, and a locals object, compile the template and generate the result. | ||
* @param {String} file A path to a template file. | ||
* @param {Object} [locals={}] The locals to the pass to the template. | ||
* @return {Promise} A promise. | ||
*/ | ||
const executeTemplate = (file, locals = {}) => new Promise((resolve, reject) => { | ||
readFile(join(process.cwd(), 'devops', 'templates', file), 'utf-8', (err, data) => { | ||
/* eslint-disable padded-blocks */ | ||
if (err) { | ||
return reject(err); | ||
} | ||
/* eslint-enable padded-blocks */ | ||
const template = compile(data); | ||
return resolve(template(locals)); | ||
}); | ||
}); | ||
/** | ||
* Read `~/.npmrc` and retrieve the `authToken`. | ||
@@ -41,2 +67,7 @@ * @return {Promise} A promise that will resolve with the token (string). | ||
module.exports = { npmAuthToken, throwErr }; | ||
module.exports = { | ||
executeTemplate, | ||
npmAuthToken, | ||
throwErr, | ||
writeFile: promisify(writeFile), | ||
}; |
@@ -5,2 +5,10 @@ # @ideariym/cli | ||
## 1.0.0-alpha.4 | ||
Bug fix release. | ||
### Improvements | ||
- Updated `c dc env file` to work more like the original from `c.sh`. If you pass `reset` it will reset the `COMPOSE_FILE` entry to be `docker-compose.yml`. If you pass `show` and the `docker-compose.show.yml` file exists, it will set the `COMPOSE_FILE` entry to be `docker-compose.yml:docker-compose.show.yml`. | ||
## 1.0.0-alpha.3 | ||
@@ -7,0 +15,0 @@ |
{ | ||
"name": "@idearium/cli", | ||
"version": "1.0.0-alpha.4", | ||
"version": "1.0.0-alpha.5", | ||
"description": "The Idearium cli, which makes working with our projects much easier.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
13466
1
247