diff --git a/CHANGELOG.md b/CHANGELOG.md index e17944a7..dc147718 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how * Adapt `-a`/`--all` option for archives command (#83) * Merge command `list-all` to list with `-a`/`--all` option (#84) * Support JSON format with Eask-file linter (#85) +* Handle multiple Eask-files for `init` command (#86) ## 0.7.x > Released Sep 08, 2022 diff --git a/cmds/core/init.js b/cmds/core/init.js index 3ce5c821..202cc881 100644 --- a/cmds/core/init.js +++ b/cmds/core/init.js @@ -23,8 +23,6 @@ const path = require('path'); const fs = require('fs'); const readline = require('readline'); -var EASK_FILE; - var instance; /* `readline` instance */ exports.command = ['init']; @@ -41,13 +39,53 @@ async function create_eask_file(dir) { output: process.stdout }); - EASK_FILE = path.join(process.cwd(), '/Eask'); - - if (fs.existsSync(EASK_FILE) || fs.existsSync(EASK_FILE + 'file')) { - console.log('Eask-file is already exists'); - process.exit(0); + let new_name = path.join(process.cwd(), 'Eask'); + + // Search for existing Eask-files! + let files = fs.readdirSync(process.cwd()).filter(fn => fn.match('Eask')); + let contine_op = false; + + if (files.length != 0) { + // Print out all existing Eask-files, and ask for continuation! + console.log('Eask-file is already exists,'); + console.log(''); + for (let index in files) { + console.log(' ' + path.join(process.cwd(), files[index])); + } + console.log(''); + await ask(`Continue the initialization? (yes) `, (answer) => { contine_op = answer; }); + + // Abort if declined! + if (contine_op != '' && contine_op != 'yes') { + process.exit(0); + } + + // Ask for new name unitl the filename is available! + let new_basename = path.basename(new_name); + let invalid_name = false; + + // Ask for new name until we found one that meets our requirements! + while (fs.existsSync(new_name) || invalid_name) { + let prompt; + + // Handle invalid file name! + if (invalid_name) { + prompt = `[?] File name '${new_basename}' is invalid (should start with 'Eask'), `; + } else { + prompt = `[?] File name '${new_basename}' already taken, `; + } + + // Ask for new name! + await ask(prompt + `try another one: `, + (answer) => { + new_name = path.join(process.cwd(), answer); + new_basename = answer; + invalid_name = !answer.startsWith('Eask'); + }); + } } + // Starting writing Eask-file! let name, version, description, entry_point, emacs_version, website_url, keywords; await ask(`package name: (${basename}) `, (answer) => { name = answer || basename; }); await ask(`version: (1.0.0) `, (answer) => { version = answer || '1.0.0'; }); @@ -75,14 +113,14 @@ async function create_eask_file(dir) { (depends-on "emacs" "${emacs_version}") `; - await ask(`About to write to ${EASK_FILE}: + await ask(`About to write to ${new_name}: ${content} Is this OK? (yes) `, (answer) => { if (answer == '' || answer == 'yes') { - fs.writeFile(EASK_FILE, content, (err) => { if (err) console.log(err); }); + fs.writeFile(new_name, content, (err) => { if (err) console.log(err); }); } }); instance.close();