Skip to content

Commit

Permalink
fix getoptionsfromjson
Browse files Browse the repository at this point in the history
  • Loading branch information
MilapNaik committed Mar 5, 2024
1 parent 053a3f4 commit fa07787
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
22 changes: 7 additions & 15 deletions src/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ describe('util', () => {
});
});

describe.only('getOptionsFromJson', () => {
describe('getOptionsFromJson', () => {
beforeEach(() => {
fsExtra.ensureDirSync(rootDir);
process.chdir(rootDir);
Expand All @@ -465,24 +465,16 @@ describe('util', () => {
});
});

it(`can't find file`, () => {
fsExtra.writeJsonSync(s`${rootDir}/rokudeployed.json`, { host: '1.2.3.4' });
expect(
util.getOptionsFromJson({ cwd: rootDir })
).to.eql(undefined);

});

it(`loads cwd from process`, () => {
try {
fsExtra.writeJsonSync(s`${process.cwd()}/rokudeploy.json`, { host: '1.2.3.4' });
expect(
mainSceneEntries,
`Should only be one files entry for 'components/MainScene.brs'`
).to.be.lengthOf(1);
expect(s`${mainSceneEntries[0].src}`).to.eql(s`${thisRootDir}/../.tmp/MainScene.brs`);
util.getOptionsFromJson()
).to.eql({
host: '1.2.3.4'
});
} finally {
//clean up
await fsExtra.remove(s`${thisRootDir}/../`);
fsExtra.removeSync(s`${process.cwd()}/rokudeploy.json`);
}

});
Expand Down
36 changes: 19 additions & 17 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,23 +449,25 @@ export class Util {
const cwd = options?.cwd ?? process.cwd();
const configPath = path.join(cwd, 'rokudeploy.json');

let configFileText = fsExtra.readFileSync(configPath).toString();
let parseErrors = [] as ParseError[];
fileOptions = parseJsonc(configFileText, parseErrors, {
allowEmptyContent: true,
allowTrailingComma: true,
disallowComments: false
});
if (parseErrors.length > 0) {
throw new Error(`Error parsing "${path.resolve(configPath)}": ` + JSON.stringify(
parseErrors.map(x => {
return {
message: printParseErrorCode(x.error),
offset: x.offset,
length: x.length
};
})
));
if (fsExtra.existsSync(configPath)) {
let configFileText = fsExtra.readFileSync(configPath).toString();
let parseErrors = [] as ParseError[];
fileOptions = parseJsonc(configFileText, parseErrors, {
allowEmptyContent: true,
allowTrailingComma: true,
disallowComments: false
});
if (parseErrors.length > 0) {
throw new Error(`Error parsing "${path.resolve(configPath)}": ` + JSON.stringify(
parseErrors.map(x => {
return {
message: printParseErrorCode(x.error),
offset: x.offset,
length: x.length
};
})
));
}
}
return fileOptions;
}
Expand Down

0 comments on commit fa07787

Please sign in to comment.