Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

object-write-to-file

pyrinelaw15MIT0.0.3

将对象写入文件中

write-to, to-file, write-to-file, object-to-file, object-write-to-file, object, file

readme

object-write-to-file

将对象写入文件中

使用示例

const path = require('path');
const ObjectWriteToFile = require('object-write-to-file');

const data = {
    a: 'a',
    b: 'b',
    c: 'c',
    1: 1,
    'a.b.c': 'a.b.c',
    tips: {
        success: 'success',
        warning: 'warning',
        error: 'error',
        obj: {
            a: 'a',
            b: 'b',
            c: 'c',
            subObj1: {
                'sub-a': 'sub-a',
                'sub-b': 'sub-b',
                'sub-c': 'sub-c',
            },
            'subObj-2': {
                'sub-a': 'sub-a',
                'sub-b': 'sub-b',
                'sub-c': 'sub-c',
            },
        }
    },
    action: {
        confirm: 'confirm',
        cancel: 'cancel',
    }
};

new ObjectWriteToFile(data, path.join(__dirname, 'output/a/b/c1/index.json')).output().then(res => {
    console.log('写入 json 文件成功');
}).catch(error => {
    console.warn(error);
    console.log('写入 json 文件异常');
});

new ObjectWriteToFile(data, path.join(__dirname, 'output/a/b/c1/index.js')).output().then(res => {
    console.log('写入 js 文件成功');
}).catch(error => {
    console.warn(error);
    console.log('写入 js 文件异常');
});

参数说明

ObjectWriteToFile(data, file);

参数 类型 是否必传 默认值 说明
data Object undefined 写入对象
file String undefined 被写入文件路径,写入 js 文件中可以直接被引用