Skip to content

Commit

Permalink
feat(build): read outDir from tsconfig when copying resources
Browse files Browse the repository at this point in the history
When `lb-tsc` is invoked in such way that `outDir` is not inferred
automatically, and no `--outDir` argument was provided via CLI args,
then read `outDir` from the tsconfig project file.

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
  • Loading branch information
bajtos committed Jun 10, 2019
1 parent 4c1b7f6 commit 4f947a3
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions packages/build/bin/compile-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ function run(argv, options) {

if (outDir) {
args.push('--outDir', path.relative(cwd, outDir));
}

if (isCopyResourcesSet) {
// Since outDir is set, ts files are compiled into that directory.
// If copy-resources flag is passed, copy resources (non-ts files)
// to the same outDir as well.
if (isCopyResourcesSet) {
copyResources(rootDir, packageDir, tsConfigFile, outDir, options);
}
copyResources(rootDir, packageDir, tsConfigFile, outDir, options);
}

if (target) {
Expand All @@ -155,26 +155,46 @@ module.exports = run;
if (require.main === module) run(process.argv);

function copyResources(rootDir, packageDir, tsConfigFile, outDir, options) {
if (rootDir && tsConfigFile) {
const tsConfig = require(tsConfigFile);
const dirs = tsConfig.include
? tsConfig.include.join('|')
: ['src', 'test'].join('|');

const compilerRootDir =
(tsConfig.compilerOptions && tsConfig.compilerOptions.rootDir) || '';

const pattern = `@(${dirs})/**/!(*.ts)`;
const files = glob.sync(pattern, {root: packageDir, nodir: true});
for (const file of files) {
/**
* Trim path that matches tsConfig.compilerOptions.rootDir
*/
let targetFile = file;
if (compilerRootDir && file.startsWith(compilerRootDir + '/')) {
targetFile = file.substring(compilerRootDir.length + 1);
}
fse.copySync(path.join(packageDir, file), path.join(outDir, targetFile));
if (!rootDir) {
console.warn('Ignoring --copy-resources option - rootDir was not set.');
return;
}
if (!tsConfigFile) {
console.warn(
'Ignoring --copy-resources option - no tsconfig file was found.',
);
return;
}

const tsConfig = require(tsConfigFile);

if (!outDir) {
outDir = tsConfig.compilerOptions && tsConfig.compilerOptions.outDir;
if (!outDir) {
console.warn(
'Ignoring --copy-resources option - outDir was not configured.',
);
return;
}
}

const dirs = tsConfig.include
? tsConfig.include.join('|')
: ['src', 'test'].join('|');

const compilerRootDir =
(tsConfig.compilerOptions && tsConfig.compilerOptions.rootDir) || '';

const pattern = `@(${dirs})/**/!(*.ts)`;
const files = glob.sync(pattern, {root: packageDir, nodir: true});
for (const file of files) {
/**
* Trim path that matches tsConfig.compilerOptions.rootDir
*/
let targetFile = file;
if (compilerRootDir && file.startsWith(compilerRootDir + '/')) {
targetFile = file.substring(compilerRootDir.length + 1);
}
fse.copySync(path.join(packageDir, file), path.join(outDir, targetFile));
}
}

0 comments on commit 4f947a3

Please sign in to comment.