Make a TIME Management Tool for the CLI (Part 4)

This is the fifth day of my participation in the August More Text Challenge. For details, see “August More Text Challenge”.

preface

The previous article mainly elaborated:

  • Batch markDown to JSON logic
  • Merge the contents of multiple MD records and sort them by time

This article will describe the development in detail:

  • Initialize the project template directive:timec init <projectName>
  • Initialize the record template instruction:timec create <filename>

The function development

Initialize the template project

The paper

User input simple instructions, can automatically create a time management template project

The template engineering structure is as follows

Project ├ ─ ─ the README. Md ├ ─ ─ study | └ ─ ─ the README. Md └ ─ ─ work └ ─ ─ the README, mdCopy the code

The working process

  1. Terminal input instruction
  2. Gets the command execution directory (CMD) and the name of the project entered by the user
  3. usingexistsSyncCheck whether the directory exists
    1. Directory does not exist, usefs.mkdirSyncCreate a directory
  4. throughfs.writeFileSyncCreate a file and write the file to the user’s current directory
    1. File content from the specified static resource directory (relative path)

implementation

To register the init directive:

  1. usecommander.commandAPI registration
  2. Get the Settings from the action callback<projectName>parameter
/** initialize the project */
commander.command("init <projectName>")
    .alias('i')
    .description('init project')
    .action((projectName) = > {
        if (initProject(cwd, projectName)) {
            console.log(Initialization `${projectName}Successful `);
            return
        }
        console.log(`${projectName}Existing `);
    })
Copy the code

Next is the concrete initProject logic:

  • CMD:process.cwd()Instruction execution directory
  • ProjectName: projectName entered by the user
  1. Determines whether the target directory exists
    1. If no, create it
    2. If there is, throwexistingwarning
  2. Read in the contents of the template file first
  3. throughcreateDirMethod to create a directory
  4. throughcreateFileMethod to write the contents to the target directory
const path = require('path')
// Static resource directory
const assetsDir = path.resolve(__dirname, 'assets')

const readmeContent = getFileContent(path.resolve(assetsDir, 'README.md'))
const demoContent = getFileContent(path.resolve(assetsDir, 'demo.md'))

/** * initialize a template project *@param {string} CWD Project directory *@param {string} ProjectName projectName */
function initProject(cwd, projectName) {
    const dir = path.resolve(cwd, projectName)
    // Create a directory
    if (createDir(dir)) {
        createFile(path.resolve(dir, 'README.md'), readmeContent)

        createDir(path.resolve(dir, 'work'))
        createDir(path.resolve(dir, 'study'))

        createFile(path.resolve(dir, 'work'.'README.md'), demoContent)
        createFile(path.resolve(dir, 'study'.'README.md'), demoContent)
        return true
    }

    return false
}

/** * create a directory * that does not exist@param {string} path 
 */
function createDir(path) {
    if(! fs.existsSync(path)) { fs.mkdirSync(path, {recursive: true })
        return true
    }
    console.error(`${path}Existing `);
    return false
}
Copy the code

This completes the process of initializing a template project

Initialize the record template

If you have a template project, you have a template file

This section describes the process of generating a template file

First, register the create

directive

/** * create a time record template file
commander.command("create <filename>")
    .alias('c')
    .description('create template note file')
    .action((filename) = > {
        if (createTemplateFIle(cwd, filename)) {
            console.log(`${filename}Created successfully ');
            return
        }
        console.log(`${filename}Existing `);
    })
Copy the code

The main logic is in the createTemplateFIle method

The idea here is simple, because the default directory to create the template file is the directory of the command command

A single line of code completes the writing of the file

/** * initialize a template record file@param {string} CWD File directory *@param {string} Filename filename */
function createTemplateFIle(cwd, filename) {
    return createFile(path.resolve(cwd, filename), demoContent)
}
Copy the code

This effect

other

The next issue is generated based on the existing functions of the entire weekly report (dove issue).

Because of the limited free time each day, this article will stop there

If you still feel more than enough, please stay tuned for further updates, or check out the warehouse first

Welcome to comment section to raise demand, exchange discussion

This series will continue to be updated and iterated until the first generation of the product is completed

  • The warehouse address