Skip to content

Commit

Permalink
fix(build): auto fix type def generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Nov 12, 2018
1 parent d2a61b6 commit a3b9056
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import packageJson from '../package.json';
import { IRollupWatchOptions } from './interfaces';
import * as path from 'path';
import ChildProcess from 'child_process';
import * as fs from 'fs';

const BASE_DIR = process.cwd();
const DIST_DIR = path.join(BASE_DIR, 'dist');
Expand Down Expand Up @@ -131,7 +132,7 @@ if (args.dev) {

async function generateDts(): Promise<void> {
console.log('\n==============\nGenerating dts bundle...\n==============');
return new Promise<void>(resolve => {
return new Promise<void>((resolve, reject) => {
ChildProcess.exec(`npm run build:dts`, (err, stdout, stderr) => {
if (err || stderr) {
console.log('Generating dts error:');
Expand All @@ -140,11 +141,25 @@ async function generateDts(): Promise<void> {
console.log('Generated dts bundle successfully');
console.log(stdout);
}
try {
fixI18nDefaultImport(path.resolve(DIST_DIR, TYPE_DIST_FILE_NAME));
} catch (ex) {
console.log('Failure fixing default import.');
reject(ex);
}
resolve();
});
});
}

async function fixI18nDefaultImport(typeDefFileName: string) {
const importDeclaration = `import i18next from "i18next";\n`;
const data = fs.readFileSync(typeDefFileName, 'utf-8');
const fd = fs.openSync(typeDefFileName, 'w+');
fs.writeSync(fd, Buffer.from(importDeclaration, 'utf8'), 0, importDeclaration.length, 0);
fs.writeSync(fd, Buffer.from(data, 'utf8'), 0, data.length, importDeclaration.length);
}

function copyToTargetProject(targetFormats: string[], targetProject: string) {
console.log('=============\nCopying to target\n=============');
targetFormats.forEach((targetFormat) => {
Expand Down

0 comments on commit a3b9056

Please sign in to comment.