Skip to content

Latest commit

聽

History

History
68 lines (49 loc) 路 1.6 KB

File metadata and controls

68 lines (49 loc) 路 1.6 KB

Command

A command is an object with two key methods, "do" which performs an action, and "undo" which rolls back
any actions taken by the "do" method.

Config

Optional properties are denoted by *

Type: object literal

logger*

Type: Logger
Default: processStdoutLogger

Creating a Custom Command

Here's an example showing how to create a command:

const fs = require("fs");
const { SDK } = require("@abstracter/atomic-release");

class CreateFolderCommand extends SDK.Command {
    constructor(options) {
        super(options);
        
        this.createdFolder = false;
    }

    /**
     * This method deletes the created dirPath
     *
     * return value is a promise for a void/undefined
     */
    async undo() {
        if (this.createdFolder) {
            await fs.promises.rmdir(this.options.dirPath, { recursive: true });
        
            this.logger.info(`Deleted created folder ${this.options.dirPath}`);
        }
    }
    
    /**
     * This method will create a directory path (like mkdir -p)
     *
     * return value is a promise for a void/undefined
     */
    async do() {
        await fs.promises.mkdir(this.options.dirPath, { recursive: true });
        
        this.logger.info(`Created ${this.options.dirPath}`);
        
        this.createdFolder = true;
    }
}

// Usage example:
// ==============
const command = new CreateFolderCommand({ dirPath: `${process.cwd()}/this/is/sparta` });

// This will create the 'dirPath'
await command.do();

// This will delete the created 'dirPath'
await command.undo();