Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import * as write from 'write-json-file';
// Basic
write('foo.json', {foo: true}).then(() => { });
write.sync('foo.json', {foo: true});
// With options
const options: write.Options = {
indent: ' ',
detectIndent: true,
sortKeys: true,
mode: 0o666
};
write('foo.json', {foo: true}, options).then(() => { });
write.sync('foo.json', {foo: true}, options);
// JSON.stringify replacer
function replacer(key: any, value: any) {
// Filtering out properties
if (typeof value === 'string') {
return undefined;
}
return value;
}
write.sync('foo.json', {foo: true}, {replacer});
write.sync('foo.json', {foo: true}, {replacer: null});
write.sync('foo.json', {foo: true}, {replacer: [1, 2, 3]});
write.sync('foo.json', {foo: true}, {replacer: ['a', 'b', 'c']});
JSON.stringify({foo: true}, replacer);
// Main processing
const quadkey = tilebelt.tileToQuadkey(tile);
const features = sources.qatiles.osm.features;
const highways = qaTilesFilter(features);
const intersects = intersections(highways);
const index = createIndex(intersects);
// Create folder if does not exist
if (!fs.existsSync(output)) mkdirp.sync(output);
// Output Features for Testing purposes
if (debug) {
const debugPath = path.join(output, quadkey) + path.sep;
// GeoJSON
write.sync(debugPath + 'highways.geojson', featureCollection(highways));
write.sync(debugPath + 'intersects.geojson', uniques2features(intersects));
write.sync(debugPath + 'tile.geojson', feature(tilebelt.tileToGeoJSON(tile)));
// Names
const names = new Set([]);
const namesEn = new Set([]);
const namesFr = new Set([]);
highways.forEach(feature => {
if (feature.properties.name) names.add(feature.properties.name);
if (feature.properties.ref) names.add(feature.properties.ref);
if (feature.properties['name:en']) namesEn.add(feature.properties['name:en']);
if (feature.properties['name:fr']) namesFr.add(feature.properties['name:fr']);
});
write.sync(debugPath + 'names.json', set2json(names));
write.sync(debugPath + 'names:en.json', set2json(namesEn));
write.sync(debugPath + 'names:fr.json', set2json(namesFr));
const quadkey = tilebelt.tileToQuadkey(tile);
const features = sources.qatiles.osm.features;
const highways = qaTilesFilter(features);
const intersects = intersections(highways);
const index = createIndex(intersects);
// Create folder if does not exist
if (!fs.existsSync(output)) mkdirp.sync(output);
// Output Features for Testing purposes
if (debug) {
const debugPath = path.join(output, quadkey) + path.sep;
// GeoJSON
write.sync(debugPath + 'highways.geojson', featureCollection(highways));
write.sync(debugPath + 'intersects.geojson', uniques2features(intersects));
write.sync(debugPath + 'tile.geojson', feature(tilebelt.tileToGeoJSON(tile)));
// Names
const names = new Set([]);
const namesEn = new Set([]);
const namesFr = new Set([]);
highways.forEach(feature => {
if (feature.properties.name) names.add(feature.properties.name);
if (feature.properties.ref) names.add(feature.properties.ref);
if (feature.properties['name:en']) namesEn.add(feature.properties['name:en']);
if (feature.properties['name:fr']) namesFr.add(feature.properties['name:fr']);
});
write.sync(debugPath + 'names.json', set2json(names));
write.sync(debugPath + 'names:en.json', set2json(namesEn));
write.sync(debugPath + 'names:fr.json', set2json(namesFr));
it("is set from lerna.json config", async () => {
const lernaJsonLocation = path.join(testDir, "lerna.json");
const lernaConfig = await loadJsonFile(lernaJsonLocation);
lernaConfig.loglevel = "warn";
await writeJsonFile(lernaJsonLocation, lernaConfig, { indent: 2 });
const ok = new OkCommand([], {}, testDir);
await ok.run();
expect(log.level).toBe("warn");
});
});
writeCachesToFile(path, data) {
if (fs_1.existsSync(path_1.dirname(path))) {
writeJsonToFile.sync(path, data);
this.logger.log("Write caches to file: " + path);
}
else {
this.logger.warn(`Path: \`${path}\` do not exists`);
}
}
}
database = {
type: engine,
database: "./notadd.sqlite",
entities: [
"**/*.entity.js",
],
migrations: [
"**/*.migration.js",
],
logging: true,
migrationsRun: false,
synchronize: false,
};
break;
}
writeJsonFile.sync(join(process.cwd(), "configurations", "database.json"), database, {
indent: 4,
sortKeys: true,
});
let wanted = "";
switch (engine) {
case "mysql":
wanted = "mysql";
break;
case "sqlite":
wanted = "sqlite3";
break;
default:
wanted = "pg";
break;
}
const writeToConfigFile = stuff =>
writeJSON.sync(CONFIG_FILE_PATH, stuff, { indent : 2 })
const writeSecretsFile = (secrets) => {
writeJson.sync(SECRETS_FILE_PATH, secrets);
return secrets;
};
protected writeCachesToFile(path: string, data: any) {
if (existsSync(dirname(path))) {
writeJsonToFile.sync(path, data);
this.logger.log("Write caches to file: " + path);
} else {
this.logger.warn(`Path: \`${path}\` do not exists`);
}
}
}