Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('handles valid case', () => {
// call this before mocking fs since it internally uses a dynamic require,
// which doesn't work while fs is mocked (but if it's called before mocking,
// the required file will be cached)
jju.update('{}', {});
mockfs({
root1: { 'rush.json': rushJsonStrNoProjects }, // no projects yet
root2: { 'rush.json': rushJsonStr } // already has a project
});
rushAddPackage('a', 'root1');
expect(fs.readFileSync('root1/rush.json').toString()).toBe(rushJsonStr);
rushAddPackage('b', 'root2');
expect(fs.readFileSync('root2/rush.json').toString()).toBe(rushJsonUpdatedStr);
});
export function _parseRushJson(rushJsonContents: string): RushJson | undefined {
try {
// rush.json can contain comments, so we have to use a json library which supports comments
// (instead of JSON.parse/JSON.stringify)
return jju.parse(rushJsonContents, { mode: 'cjson' });
} catch {
return undefined;
}
}
public static load(jsonFilename: string): any {
// tslint:disable-line:no-any
if (!FileSystem.exists(jsonFilename)) {
throw new Error(`Input file not found: ${jsonFilename}`);
}
const contents: string = FileSystem.readFile(jsonFilename);
try {
return jju.parse(contents);
} catch (error) {
throw new Error(`Error reading "${jsonFilename}":` + os.EOL + ` ${error.message}`);
}
}
public static updateString(
previousJson: string,
newJsonObject: Object,
options?: IJsonFileStringifyOptions
): string {
if (!options) {
options = {};
}
JsonFile.validateNoUndefinedMembers(newJsonObject);
let stringified: string;
if (previousJson !== '') {
// NOTE: We don't use mode=json here because comments aren't allowed by strict JSON
stringified = jju.update(previousJson, newJsonObject, {
mode: 'cjson',
indent: 2
});
} else if (options.prettyFormatting) {
stringified = jju.stringify(newJsonObject, {
mode: 'json',
indent: 2
});
} else {
stringified = JSON.stringify(newJsonObject, undefined, 2);
}
// Add the trailing newline
stringified = Text.ensureTrailingNewline(stringified);
if (options && options.newlineConversion) {
if (!options) {
options = {};
}
JsonFile.validateNoUndefinedMembers(newJsonObject);
let stringified: string;
if (previousJson !== '') {
// NOTE: We don't use mode=json here because comments aren't allowed by strict JSON
stringified = jju.update(previousJson, newJsonObject, {
mode: 'cjson',
indent: 2
});
} else if (options.prettyFormatting) {
stringified = jju.stringify(newJsonObject, {
mode: 'json',
indent: 2
});
} else {
stringified = JSON.stringify(newJsonObject, undefined, 2);
}
// Add the trailing newline
stringified = Text.ensureTrailingNewline(stringified);
if (options && options.newlineConversion) {
switch (options.newlineConversion) {
case NewlineKind.CrLf:
return Text.convertToCrLf(stringified);
case NewlineKind.Lf:
return Text.convertToLf(stringified);
it('parses file', () => {
const rushJsonParsed = jju.parse(rushJsonStr, { mode: 'cjson' });
expect(_parseRushJson(rushJsonStr)).toEqual(rushJsonParsed);
});
});
function on_textarea_update() {
try {
var tokens = jju.tokenize($('textarea').val())
} catch(err) {
return console.log(err)
}
function tr(html) {
return '' + html + ''
}
function td(text) {
var node = $('')
node.text(text)
return node[0].outerHTML
}
$('#tokens .data').remove()
function readConfig(file) {
file = findConfig(file);
if (file && fs.existsSync(file)) {
return jju.parse(fs.readFileSync(file, 'utf8'));
}
}
function update_deps() {
var items = $('#dependencies').val()
if (items === _old_items) return
_old_items = items
var deps = {}
var json = jju.parse($('textarea').val())
$('#dependencies').tagsinput('items').forEach(function(x) {
deps[x] = (json.dependencies && json.dependencies[x]) || '*'
})
on_input_update('dependencies', deps)
}
})()
function loadJsonFile(jsonFileName) {
try {
const buffer = fs.readFileSync(jsonFileName)
const jju = require('jju')
return jju.parse(buffer.toString())
} catch (error) {
throw new Error(
`Error reading "${jsonFileName}":${os.EOL} ${error.message}`
)
}
}