gerard2p/koaton-cli

View on GitHub
src/utils/write.js

Summary

Maintainability
A
1 hr
Test Coverage
import 'colors';
import * as fs from 'fs-extra';
import * as path from 'upath';
import writemodes from './writemodes';
import relpath from './relpath';

export default function writeSync (...args) {
    let [file, content, mode] = args;
    /* istanbul ignore next */
    if (content.indexOf('koaton:static') > -1) {
        return file;
    }
    file = path.normalize(file);
    try {
        fs.writeFileSync(file, content);
        let label;
        switch (mode) {
            case writemodes.update:
                label = 'update'.cyan + ':';
                break;
            case writemodes.create:
                label = 'create'.cyan + ':';
                break;
            default:
                label = null;
                break;
        }
        if (label !== null) {
            console.log(`   ${label} ${relpath(file)}`);
        }
        return file;
    } catch (e) {
        console.log(e.stack);
    }
    return null;
}