Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 2x 1x 1x 1x 12x 14x 14x 14x 4x 2x 2x 1x 1x 4x 10x 10x 2x 2x 8x 5x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x | import * as fs from 'fs';
import { Tree } from 'jargs';
import {
Config,
ConfigProject,
DEFAULT_ENV,
MATCHES_NO,
UTF8,
} from './constants';
import * as logger from './logger';
import {
createStringFromConfig,
delIn,
getConfigPath,
getIn,
getProjectName,
readWtfJson,
setIn,
writeConfigCallback,
} from './utils';
const envCommand = (tree: Tree) => {
let config: Config | undefined;
let projectConfig: ConfigProject | undefined;
const configPath = getConfigPath();
const projectName = getProjectName();
let { env } = tree.kwargs;
env = typeof env === 'string' ? env : tree.command && tree.command.kwargs.env;
env = typeof env === 'string' ? env : DEFAULT_ENV;
if (!fs.existsSync(configPath)) {
if (tree.command && tree.command.name === 'set') {
logger.log(`No wtf.json found at ${configPath}. I\'ll create that for you`);
} else {
logger.log(`No wtf.json found at ${configPath} - run "wtf init" to begin setup`);
process.exit(1);
}
} else {
config = readWtfJson(configPath);
}
config = config ? config : {};
projectConfig = config[projectName];
if (!tree.command) {
if (!projectConfig) {
logger.log(`No config for project ${projectName} in wtf.json at ${configPath}`);
} else if (!projectConfig.env) {
logger.log(`No environments for project ${projectName} in wtf.json at ${configPath}`);
} else {
logger.log(createStringFromConfig(projectConfig.env));
}
return process.exit(0);
}
const { key, value } = tree.command.args;
if (typeof key !== 'string') {
logger.log('No key provided');
return process.exit(1);
}
switch (tree.command.name) {
case 'set':
if (typeof value !== 'string') {
logger.log('No value provided');
return process.exit(1);
}
setIn(config, [projectName, 'env', env, key], value);
break;
case 'get':
logger.log(getIn(config, [projectName, 'env', env, key]));
return process.exit(0);
case 'del':
delIn(config, [projectName, 'env', env, key]);
break;
default:
logger.log(`Unknown command ${tree.command.name}`);
return process.exit(1);
}
const stringConfig = createStringFromConfig(getIn(config, [projectName, 'env']));
process.stdin.resume();
logger.log(`\nCreated config:\n\n${stringConfig}\nIs this correct? [y]`);
process.stdin.once('data', (data) => {
process.stdin.pause();
const input: string = (data || '').toString().trim();
if (!MATCHES_NO.test(input)) {
fs.writeFile(
configPath,
createStringFromConfig(config),
UTF8,
writeConfigCallback
);
}
});
};
export default envCommand;
|