abi-gen should regenerate contract-wrappers if template files change (#1875)
This commit is contained in:
parent
01a1b19556
commit
9775f8d83c
@ -1,4 +1,13 @@
|
|||||||
[
|
[
|
||||||
|
{
|
||||||
|
"version": "2.0.11",
|
||||||
|
"changes": [
|
||||||
|
{
|
||||||
|
"note": "Watch template files for changes",
|
||||||
|
"pr": 1875
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"timestamp": 1557507213,
|
"timestamp": 1557507213,
|
||||||
"version": "2.0.10",
|
"version": "2.0.10",
|
||||||
|
@ -58,8 +58,12 @@ const args = yargs
|
|||||||
'Full usage example',
|
'Full usage example',
|
||||||
).argv;
|
).argv;
|
||||||
|
|
||||||
function registerPartials(partialsGlob: string): void {
|
const mainTemplate = utils.getNamedContent(args.template);
|
||||||
const partialTemplateFileNames = globSync(partialsGlob);
|
const template = Handlebars.compile<ContextData>(mainTemplate.content);
|
||||||
|
const abiFileNames = globSync(args.abis);
|
||||||
|
const partialTemplateFileNames = globSync(args.partials);
|
||||||
|
|
||||||
|
function registerPartials(): void {
|
||||||
logUtils.log(`Found ${chalk.green(`${partialTemplateFileNames.length}`)} ${chalk.bold('partial')} templates`);
|
logUtils.log(`Found ${chalk.green(`${partialTemplateFileNames.length}`)} ${chalk.bold('partial')} templates`);
|
||||||
for (const partialTemplateFileName of partialTemplateFileNames) {
|
for (const partialTemplateFileName of partialTemplateFileNames) {
|
||||||
const namedContent = utils.getNamedContent(partialTemplateFileName);
|
const namedContent = utils.getNamedContent(partialTemplateFileName);
|
||||||
@ -70,12 +74,7 @@ function registerPartials(partialsGlob: string): void {
|
|||||||
Handlebars.registerHelper('parameterType', utils.solTypeToTsType.bind(utils, ParamKind.Input, args.backend));
|
Handlebars.registerHelper('parameterType', utils.solTypeToTsType.bind(utils, ParamKind.Input, args.backend));
|
||||||
Handlebars.registerHelper('assertionType', utils.solTypeToAssertion.bind(utils));
|
Handlebars.registerHelper('assertionType', utils.solTypeToAssertion.bind(utils));
|
||||||
Handlebars.registerHelper('returnType', utils.solTypeToTsType.bind(utils, ParamKind.Output, args.backend));
|
Handlebars.registerHelper('returnType', utils.solTypeToTsType.bind(utils, ParamKind.Output, args.backend));
|
||||||
if (args.partials) {
|
registerPartials();
|
||||||
registerPartials(args.partials);
|
|
||||||
}
|
|
||||||
const mainTemplate = utils.getNamedContent(args.template);
|
|
||||||
const template = Handlebars.compile<ContextData>(mainTemplate.content);
|
|
||||||
const abiFileNames = globSync(args.abis);
|
|
||||||
|
|
||||||
if (_.isEmpty(abiFileNames)) {
|
if (_.isEmpty(abiFileNames)) {
|
||||||
logUtils.log(`${chalk.red(`No ABI files found.`)}`);
|
logUtils.log(`${chalk.red(`No ABI files found.`)}`);
|
||||||
@ -109,7 +108,7 @@ for (const abiFileName of abiFileNames) {
|
|||||||
const outFileName = utils.makeOutputFileName(namedContent.name);
|
const outFileName = utils.makeOutputFileName(namedContent.name);
|
||||||
const outFilePath = `${args.output}/${outFileName}.ts`;
|
const outFilePath = `${args.output}/${outFileName}.ts`;
|
||||||
|
|
||||||
if (utils.isOutputFileUpToDate(abiFileName, outFilePath)) {
|
if (utils.isOutputFileUpToDate(outFilePath, [abiFileName, args.template, ...partialTemplateFileNames])) {
|
||||||
logUtils.log(`Already up to date: ${chalk.bold(outFilePath)}`);
|
logUtils.log(`Already up to date: ${chalk.bold(outFilePath)}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -138,11 +138,11 @@ export const utils = {
|
|||||||
writeOutputFile(filePath: string, renderedTsCode: string): void {
|
writeOutputFile(filePath: string, renderedTsCode: string): void {
|
||||||
fs.writeFileSync(filePath, renderedTsCode);
|
fs.writeFileSync(filePath, renderedTsCode);
|
||||||
},
|
},
|
||||||
isOutputFileUpToDate(abiFile: string, outputFile: string): boolean {
|
isOutputFileUpToDate(outputFile: string, sourceFiles: string[]): boolean {
|
||||||
const abiFileModTimeMs = fs.statSync(abiFile).mtimeMs;
|
const sourceFileModTimeMs = sourceFiles.map(file => fs.statSync(file).mtimeMs);
|
||||||
try {
|
try {
|
||||||
const outFileModTimeMs = fs.statSync(outputFile).mtimeMs;
|
const outFileModTimeMs = fs.statSync(outputFile).mtimeMs;
|
||||||
return outFileModTimeMs > abiFileModTimeMs;
|
return sourceFileModTimeMs.find(sourceMs => sourceMs > outFileModTimeMs) === undefined;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === 'ENOENT') {
|
if (err.code === 'ENOENT') {
|
||||||
return false;
|
return false;
|
||||||
|
@ -42,7 +42,7 @@ describe('writeOutputFile()', () => {
|
|||||||
|
|
||||||
describe('isOutputFileUpToDate()', () => {
|
describe('isOutputFileUpToDate()', () => {
|
||||||
it('should throw ENOENT when there is no abi file', () => {
|
it('should throw ENOENT when there is no abi file', () => {
|
||||||
expect(utils.isOutputFileUpToDate.bind('nonexistant1', 'nonexistant2')).to.throw('ENOENT');
|
expect(utils.isOutputFileUpToDate.bind('', 'nonexistant1', ['nonexistant2'])).to.throw('ENOENT');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when the abi input file exists', () => {
|
describe('when the abi input file exists', () => {
|
||||||
@ -55,7 +55,7 @@ describe('isOutputFileUpToDate()', () => {
|
|||||||
|
|
||||||
describe('without an existing output file', () => {
|
describe('without an existing output file', () => {
|
||||||
it('should return false', () => {
|
it('should return false', () => {
|
||||||
expect(utils.isOutputFileUpToDate(abiFile, 'nonexistant_file')).to.be.false();
|
expect(utils.isOutputFileUpToDate('nonexistant_file', [abiFile])).to.be.false();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ describe('isOutputFileUpToDate()', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return true when output file is newer than abi file', async () => {
|
it('should return true when output file is newer than abi file', async () => {
|
||||||
expect(utils.isOutputFileUpToDate(abiFile, outputFile)).to.be.true();
|
expect(utils.isOutputFileUpToDate(outputFile, [abiFile])).to.be.true();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false when output file exists but is older than abi file', () => {
|
it('should return false when output file exists but is older than abi file', () => {
|
||||||
@ -79,7 +79,19 @@ describe('isOutputFileUpToDate()', () => {
|
|||||||
const abiFileModTimeMs = outFileModTimeMs + 1;
|
const abiFileModTimeMs = outFileModTimeMs + 1;
|
||||||
fs.utimesSync(abiFile, abiFileModTimeMs, abiFileModTimeMs);
|
fs.utimesSync(abiFile, abiFileModTimeMs, abiFileModTimeMs);
|
||||||
|
|
||||||
expect(utils.isOutputFileUpToDate(abiFile, outputFile)).to.be.false();
|
expect(utils.isOutputFileUpToDate(outputFile, [abiFile])).to.be.false();
|
||||||
|
});
|
||||||
|
it('should return false when any source file is newer than output file', () => {
|
||||||
|
const templateFile = tmp.fileSync(
|
||||||
|
{ discardDescriptor: true }, // close file (set timestamp)
|
||||||
|
).name;
|
||||||
|
|
||||||
|
const templateFileModTimeMs = fs.statSync(outputFile).mtimeMs + 1;
|
||||||
|
const abiFileModTimeMs = fs.statSync(outputFile).mtimeMs;
|
||||||
|
fs.utimesSync(templateFile, templateFileModTimeMs, templateFileModTimeMs);
|
||||||
|
fs.utimesSync(abiFile, abiFileModTimeMs, abiFileModTimeMs);
|
||||||
|
|
||||||
|
expect(utils.isOutputFileUpToDate(outputFile, [abiFile, templateFile])).to.be.false();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user