本文整理汇总了TypeScript中jsonfile.writeFile函数的典型用法代码示例。如果您正苦于以下问题:TypeScript writeFile函数的具体用法?TypeScript writeFile怎么用?TypeScript writeFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了writeFile函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getSettings
export function getSettings():Isettings{
//Give some default values
let settings:Isettings = {
port : getRandomPort()
};
// *******************************************
// Checks that iisexpress.json exist
// *******************************************
let settingsFilePath = vscode.workspace.rootPath + "\\.vscode\\iisexpress.json";
//use -> https://www.npmjs.com/package/jsonfile
var jsonfile = require('jsonfile');
try {
//Check if we can find the file path (get stat info on it)
let fileCheck = fs.statSync(settingsFilePath);
//read file .vscode\iisexpress.json and overwrite port property from iisexpress.json
settings = jsonfile.readFileSync(settingsFilePath);
}
catch (err) {
//file didn't exist so
//create .vscode\iisexpress.json and append settings object
jsonfile.writeFile(settingsFilePath, settings, {spaces: 2}, function (err) {
console.error(err);
});
}
//Return an object back from verifications
return settings;
}
示例2: function
ipcMain.on("save", (event, arg) => {
if (arg.pathToSave) {
jsonfile.writeFile(arg.pathToSave, arg.spec, function(err) {
if (!err) {
event.sender.send("onSaveSuccess", {
name: arg.name,
path: arg.pathToSave
});
}
});
} else {
dialog.showSaveDialog(
{
filters: [
{
name: "Json",
extensions: ["json"]
}
]
},
filename => {
if (filename) {
jsonfile.writeFile(filename, arg.spec, function(err) {
if (!err) {
event.sender.send("onSaveSuccess", {
name: arg.name,
path: filename
});
}
});
}
}
);
}
});
示例3: writetoJSON
function writetoJSON(encryptedUsername, encryptedPassword) {
console.log("encrypted username is: " + encryptedUsername);
var file = 'data.json';
var obj = {'username': encryptedUsername.toString(), 'password': encryptedPassword.toString()};
jsonfile.writeFile(file, obj, function (err) {
if (err) throw err;
console.log('username and password succesfullt saved');
})
}
示例4:
jsonfile.readFile(file).then(obj => {
// $ExpectType any
obj;
});
jsonfile.readFile(file, { encoding: 'utf8', throws: true }).then(obj => {
// $ExpectType any
obj;
});
// $ExpectType any
jsonfile.readFileSync(file);
jsonfile.readFileSync(file, { encoding: 'utf8', throws: true });
// $ExpectType void
jsonfile.writeFile(file, obj, err => {
// $ExpectType ErrnoException
err;
});
// $ExpectType void
jsonfile.writeFile(file, obj, { spaces: 2 }, err => {
// $ExpectType ErrnoException
err;
});
// $ExpectType void
jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, err => {
// $ExpectType ErrnoException
err;
});
// $ExpectType void
示例5:
// Following are lifted from the samples on the NPM page, modified to pass
// the linter
import * as jsonfile from 'jsonfile';
const file = '/tmp/data.json';
const obj = {name: 'JP'};
jsonfile.readFile(file, (err: NodeJS.ErrnoException | null, obj: any) => {
console.dir(obj);
});
console.dir(jsonfile.readFileSync(file));
jsonfile.writeFile(file, obj, (err: NodeJS.ErrnoException) => {
console.error(err);
});
jsonfile.writeFile(file, obj, {spaces: 2}, (err: NodeJS.ErrnoException) => {
console.error(err);
});
jsonfile.writeFile(file, obj, {spaces: 2, EOL: '\r\n'}, (err: NodeJS.ErrnoException) => {
console.error(err);
});
jsonfile.writeFile(file, obj, {flag: 'a'}, (err: NodeJS.ErrnoException) => {
console.error(err);
});
jsonfile.writeFileSync(file, obj);
示例6: writeFile
writeFile(filename: string, obj: any, options: any, cb?: (err: Error) => void): any {
return jsonfile.writeFile(filename, obj, options, cb);
}