packdir-cli
Version:
Packdir CLI
139 lines • 4.63 kB
JavaScript
/**
* Init to create packdir.json
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const promises_1 = require("fs/promises");
const path_1 = tslib_1.__importDefault(require("path"));
const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
class Init extends core_1.Command {
async run() {
this.log('This utility will walk you through creating a packdir.json file.');
this.log('It only covers the most common items, and tries to guess sensible defaults.');
this.log('');
this.log('See `pd help init` for definitive documentation on these fields');
this.log('and exactly what they do.');
this.log('');
this.log('Press ^C at any time to quit.');
const { args, flags } = await this.parse(Init);
if (flags.yes) {
this.log(`you input --yes:`);
}
const questions = await this.initQuestions();
inquirer_1.default
.prompt(questions)
.then(async (answers) => {
const config = this.generateConfig(answers);
await (0, promises_1.writeFile)('packdir.json', config);
console.log('Create packdir.json successfully!');
})
.catch((err) => {
console.log('Error: ', err);
});
// Create packdir directory
(0, promises_1.stat)('packdir').catch(async (err) => {
if (err.code === 'ENOENT') { // Directory packdir is not existing
await (0, promises_1.mkdir)('packdir', { recursive: true });
}
});
}
/**
* Get questions for init.
*
* @returns Questions for init.
*/
async initQuestions() {
const currentPathname = path_1.default.basename(process.cwd());
// Markdown list
let markdownFiles = [];
// Image list
let imageFiles = [];
const files = await (0, promises_1.readdir)('./');
files.forEach((filename) => {
const ext = path_1.default.extname(filename).toLowerCase();
if ('.md' === ext) {
markdownFiles.push({
name: filename,
checked: true,
value: filename
});
}
if ('.jpg' === ext || '.png' === ext) {
imageFiles.push({
name: filename,
checked: true,
value: filename
});
}
});
let questions = [
{
type: 'input',
name: 'doc_name',
message: 'Document name:',
default: currentPathname
},
{
type: 'input',
name: 'author',
message: 'Author:',
default: ''
},
{
type: 'checkbox',
name: 'doc_articles',
message: 'What files do you want to include?',
pageSize: 25,
choices: markdownFiles
},
];
// Choose cover image.
if (imageFiles.length > 0) {
imageFiles.push({
name: '[none]',
checked: true,
value: 'none'
});
questions.push({
type: 'list',
name: 'doc_cover',
message: 'Choose the cover image:',
pageSize: 25,
choices: imageFiles
});
}
return questions;
}
generateConfig(answers) {
let articles = [];
answers.doc_articles.forEach((filename) => {
articles.push(filename);
});
let urlcover = '';
if (answers.doc_cover && answers.doc_cover.length > 0 && answers.doc_cover !== 'none') {
const pathname = path_1.default.resolve(answers.doc_cover);
if (pathname) {
urlcover = `file://${pathname}`;
}
}
const config = {
"documentName": answers.doc_name,
"author": answers.author,
"cover": urlcover,
"content": articles
};
return JSON.stringify(config, null, 2);
}
}
exports.default = Init;
Init.description = 'Init to create packdir.json';
Init.examples = [
'<%= config.bin %> <%= command.id %>',
];
Init.flags = {
yes: core_1.Flags.boolean({ char: 'y' }),
};
Init.args = [{ name: 'file' }];
//# sourceMappingURL=init.js.map
;