I'm trying to publish to DefinitelyTyped and noticed everyone is putting up one definitions file, index.d.ts, which contains a summary of all the types.
My script instead goes through my codebase, and generates individual type files for all my components, and the index.d.ts file is just the imports and exports, no type definitions.
const glob = require('glob');
const jsc = require('jscodeshift');
const flowParser = require('jscodeshift/parser/flow');
const fs = require('fs');
const convert = require('@khanacademy/flow-to-ts/dist/convert.bundle.js');
const ts = require('typescript');
const files = glob.sync('src/**/*.js', {
root: './',
ignore: ['src/**/*.*test*.js'],
});
console.log('Files-found', files.length);
files.forEach((file) => {
const FILE_NAME = file.split('/')[file.split('/').length - 1].replace('js', 'ts');
const flowCode = fs.readFileSync(file, 'utf-8');
const ast = jsc(flowCode, {
parser: flowParser(),
});
ast
.find(jsc.GenericTypeAnnotation, {
id: { name: 'TimeoutID' },
})
.replaceWith('number');
ast
.find(jsc.GenericTypeAnnotation, {
id: { name: 'AnimationFrameID' },
})
.replaceWith('number');
ast
.find(jsc.GenericTypeAnnotation, {
id: { name: 'MediaQueryListListener' },
})
.replaceWith('MediaQueryList');
const transformedCode = ast.toSource();
console.warn('FileName', FILE_NAME);
const typescriptCode = convert.convert(transformedCode, {
printWidth: 80,
singleQuote: true,
semi: false,
prettier: true,
});
fs.writeFileSync(`typescript/${FILE_NAME}`, typescriptCode, (err) => {
console.log('Error:', err);
});
});
const tsFiles = glob.sync('typescript/**/*.ts');
console.log('tsfiles', tsFiles.length);
const options = {
declaration: true,
emitDeclarationOnly: true,
declarationDir: 'typescript/',
};
const program = ts.createProgram(tsFiles, options);
const res = program.emit();
// In case there was some error while generating declaration,
// throw an error.
console.log(res.diagnostics);
res.diagnostics.forEach((obj) => {
console.log('file: ', obj.file.fileName);
console.error('error: ', obj.messageText);
});
How do I generate only the index.d.ts file with actually definitions?