本文整理匯總了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);
}