Skip to content

Commit

Permalink
migrate to @backkit/autoconf
Browse files Browse the repository at this point in the history
  • Loading branch information
dchapkine committed Apr 4, 2023
1 parent 4fc1937 commit 3b2e52a
Show file tree
Hide file tree
Showing 3 changed files with 1,154 additions and 402 deletions.
69 changes: 17 additions & 52 deletions configure.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,19 @@
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const yaml = require('js-yaml');
const autoconf = require("@backkit/autoconf");

const skipPrompt = process.env.NO_INTERACTIVE || process.env.NO_PROMPT ? true : false;
const skipAutoconf = process.env.NO_AUTOCONF ? true : false;

const generate = (serviceName, moduleName, config) => {
const serviceDir = `${__dirname}/../../services`;
const servicePath = `${__dirname}/../../services/${serviceName}.js`;
const configDir = `${__dirname}/../../config`;
const configPath = `${__dirname}/../../config/${serviceName}.yml`;

console.log("");
console.log(`${serviceName} service config:`);
console.log(JSON.stringify(config, null, ' '));
console.log("");

// save service config
console.log(`writing config: ${configPath}`);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, {recursive: true});
}
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, yaml.safeDump(config, {skipInvalid: true}));
}

// enable service
console.log(`creating service alias: ${servicePath}`);
if (!fs.existsSync(serviceDir)) {
fs.mkdirSync(serviceDir, {recursive: true});
}
if (!fs.existsSync(servicePath)) {
fs.writeFileSync(servicePath, `module.exports = require('${moduleName}')`);
}
};

if (!skipAutoconf) {
const packageJson = require('./package.json');
const serviceName = 'logger';
const moduleName = packageJson.name;
const defaultConf = {};

if (!skipPrompt) {
const questions = [];

inquirer.prompt(questions).then(conf => {
generate(serviceName, moduleName, conf);
});
} else {
generate(serviceName, moduleName, defaultConf);
autoconf('logger')
.generator(self => ([
{
mkdirp: self.rootServiceDir
},
{
putFileOnce: self.serviceCodeMainJS,
content: `module.exports = require('${self.npmModuleName}');`
},
{
putFileOnce: self.serviceConfigMainYML,
contentYml: self.config
}
}
]))
.default(self => ({}))
.prompt(self => ([]))
.run()
Loading

0 comments on commit 3b2e52a

Please sign in to comment.