Skip to content

Commit

Permalink
fix(build): generate a tsconfig file with files
Browse files Browse the repository at this point in the history
  • Loading branch information
pathurs committed Jan 11, 2020
1 parent 71aa2cf commit 6c06b70
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 48 deletions.
61 changes: 53 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
},
"devDependencies": {
"@types/app-root-path": "^1.2.4",
"@types/cross-spawn": "^6.0.1",
"@types/glob": "^7.1.1"
"@types/cross-spawn": "^6.0.1"
},
"dependencies": {
"@semantic-release/changelog": "^3.0.6",
Expand All @@ -49,7 +48,7 @@
"chai": "^4.2.0",
"cross-spawn": "^7.0.1",
"del": "^5.1.0",
"glob": "^7.1.6",
"globby": "^11.0.0",
"mocha": "^6.2.2",
"nyc": "^14.1.1",
"semantic-release": "^15.13.31",
Expand Down
27 changes: 26 additions & 1 deletion source/build.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import * as fs from 'fs';
import { cleanBuildDirectory } from './clean';
import { executeCommand, log, wasCalledFromCLI } from './helpers';
import { executeCommand, getFilePaths, log, wasCalledFromCLI } from './helpers';
import {
absoluteRootGeneratedTsconfigJsonPath,
absoluteRootSourceTypescriptFilesGlob,
absoluteRootTsconfigJsonPath
} from './variables';

function generateTsconfig() {
const tsconfigString = fs.readFileSync(absoluteRootTsconfigJsonPath, 'utf8');
const tsconfigData = JSON.parse(tsconfigString);

const files = getFilePaths(absoluteRootSourceTypescriptFilesGlob);

tsconfigData.files = files;

const modifiedTsconfigData = JSON.stringify(tsconfigData, undefined, 4);

fs.writeFileSync(absoluteRootGeneratedTsconfigJsonPath, modifiedTsconfigData);
}

function buildTypeScript(outputDirectoryPath: string): void {
log(`Generating TSConfig file to '${absoluteRootGeneratedTsconfigJsonPath}'.`);

generateTsconfig();

log(`Building TypeScript files to '${outputDirectoryPath}' directory.`);

executeCommand('tsc', [
'--project',
absoluteRootGeneratedTsconfigJsonPath,
'--outDir',
outputDirectoryPath
]);
Expand Down
28 changes: 10 additions & 18 deletions source/distribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
absoluteRootDistributeDefaultFilesGlob,
absoluteRootDistributeEssentialDirectory,
absoluteRootDistributePackageJsonPath,
absoluteRootEssentialDirectory,
absoluteRootDirectory,
absoluteRootEssentialFilesGlobs,
renamedPrefix
} from './variables';
Expand All @@ -25,26 +25,18 @@ function copyFiles(
originDirectory: string,
destinationDirectory: string
): void {
const targetPatterns = Array.isArray(targetPatternOrTargetPatterns)
? targetPatternOrTargetPatterns
: [
targetPatternOrTargetPatterns
];
const filePaths = getFilePaths(<string[]>targetPatternOrTargetPatterns);

for (const targetPattern of targetPatterns) {
const filePaths = getFilePaths(targetPattern);
for (const filePath of filePaths) {
const relativeFilePath = path.relative(originDirectory, filePath);
const destinationFilePath = path.join(destinationDirectory, relativeFilePath);
const directory = path.dirname(destinationFilePath);

for (const filePath of filePaths) {
const relativeFilePath = path.relative(originDirectory, filePath);
const destinationFilePath = path.join(destinationDirectory, relativeFilePath);
const directory = path.dirname(destinationFilePath);
fs.mkdirSync(directory, { recursive: true });

fs.mkdirSync(directory, { recursive: true });
log(`Copying file '${filePath}' to '${destinationFilePath}'.`);

log(`Copying file '${filePath}' to '${destinationFilePath}'.`);

fs.copyFileSync(filePath, destinationFilePath);
}
fs.copyFileSync(filePath, destinationFilePath);
}
}

Expand All @@ -53,7 +45,7 @@ function copyFilesToDistributeDirectory() {
copyFiles(absoluteRootDefaultFilesGlob, absoluteRootDefaultDirectory, absoluteRootDistributeDefaultDirectory);
copyFiles(
absoluteRootEssentialFilesGlobs,
absoluteRootEssentialDirectory,
absoluteRootDirectory,
absoluteRootDistributeEssentialDirectory
);
}
Expand Down
21 changes: 14 additions & 7 deletions source/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import spawn from 'cross-spawn';
import { default as glob, IOptions } from 'glob';
import globby from 'globby';
import * as path from 'path';

export function log(message: string): void {
Expand All @@ -16,12 +16,19 @@ export function wasCalledFromCLI(otherModule: NodeModule): boolean {
return require.main === otherModule;
}

export function getFilePaths(pattern: string): string[] {
const filePaths = (<(pattern: string, options: IOptions) => string[]>(<unknown>glob))(pattern, {
dot: true,
nodir: true,
sync: true
});
export function getFilePaths(pattern: string): string[];
export function getFilePaths(patterns: string[]): string[];
export function getFilePaths(patternOrPatterns: string | string[]): string[] {
const patterns = Array.isArray(patternOrPatterns)
? patternOrPatterns
: [
patternOrPatterns
];

// https://github.com/mrmlnc/fast-glob#pattern-syntax
const fixedPatterns = patterns.map(pattern => pattern.replace(/\\/g, '/'));

const filePaths = globby.sync(fixedPatterns);

const normalizedFilePaths = filePaths.map(filePath => path.normalize(filePath));

Expand Down
27 changes: 16 additions & 11 deletions source/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';

export const relativePackageJsonPath = path.normalize('./package.json');
export const relativeTsconfigJsonPath = path.normalize('./tsconfig.json');
export const relativeGeneratedTsconfigJsonPath = path.normalize('./tsconfig.generated.json');
export const relativeTslintJsonPath = path.normalize('./tslint.json');
export const relativeLicensePath = path.normalize('./LICENSE');
export const relativeReadmeMarkdownPath = path.normalize('./README.md');
Expand All @@ -23,10 +24,9 @@ export const absoluteCurrentDirectory = __dirname.includes('node_modules')

export const absoluteRootSourceDirectory = RootPath.resolve(relativeSourceDirectory);

export const absoluteRootSourceDeclarationFileGlob = `${absoluteRootSourceDirectory}/**/*.d.ts`;
export const absoluteRootSourceTestFilesGlob = `${absoluteRootSourceDirectory}/**/*.spec.ts`;
export const absoluteRootSourceLintFilesGlobs = `${absoluteRootSourceDirectory}/**/*.ts`;
export const absoluteRootSourceTsconfigJsonPath = path.join(absoluteRootSourceDirectory, relativeTsconfigJsonPath);
export const absoluteRootSourceTestFilesGlob = path.join(absoluteRootSourceDirectory, '/**/*.spec.ts');
export const absoluteRootSourceTypescriptFilesGlob = path.join(absoluteRootSourceDirectory, '/**/*.ts');
export const absoluteRootSourceDeclarationFileGlob = path.join(absoluteRootSourceDirectory, '/**/*.d.ts');

// Build

Expand All @@ -44,15 +44,20 @@ export const absoluteRootDefaultDirectory = RootPath.resolve(relativeDefaultDire

export const absoluteRootDefaultFilesGlob = `${absoluteRootDefaultDirectory}/**/*`;

// Essential
// Root

export const absoluteRootEssentialDirectory = RootPath.path;
export const absoluteRootDirectory = RootPath.path;

export const absoluteRootPackageJsonPath = path.join(absoluteRootEssentialDirectory, relativePackageJsonPath);
export const absoluteRootTsconfigJsonPath = path.join(absoluteRootEssentialDirectory, relativeTsconfigJsonPath);
export const absoluteRootTslintJsonPath = path.join(absoluteRootEssentialDirectory, relativeTslintJsonPath);
export const absoluteRootLicensePath = path.join(absoluteRootEssentialDirectory, relativeLicensePath);
export const absoluteRootReadmeMarkdownPath = path.join(absoluteRootEssentialDirectory, relativeReadmeMarkdownPath);
export const absoluteRootPackageJsonPath = path.join(absoluteRootDirectory, relativePackageJsonPath);
export const absoluteRootTsconfigJsonPath = path.join(absoluteRootDirectory, relativeTsconfigJsonPath);
// tslint:disable-next-line: max-line-length
export const absoluteRootGeneratedTsconfigJsonPath = path.join(
absoluteRootDirectory,
relativeGeneratedTsconfigJsonPath
);
export const absoluteRootTslintJsonPath = path.join(absoluteRootDirectory, relativeTslintJsonPath);
export const absoluteRootLicensePath = path.join(absoluteRootDirectory, relativeLicensePath);
export const absoluteRootReadmeMarkdownPath = path.join(absoluteRootDirectory, relativeReadmeMarkdownPath);

export const absoluteRootEssentialFilesGlobs = [
absoluteRootPackageJsonPath,
Expand Down
43 changes: 43 additions & 0 deletions tsconfig.generated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"compilerOptions": {
"alwaysStrict": true,
"baseUrl": ".",
"declaration": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"importHelpers": true,
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"paths": {
"*": [
"node_modules/*",
"@types/*"
]
},
"strict": true,
"strictBindCallApply": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "es6"
},
"files": [
"d:\\Github\\Colonise\\Config\\source\\build.ts",
"d:\\Github\\Colonise\\Config\\source\\clean.ts",
"d:\\Github\\Colonise\\Config\\source\\cli.ts",
"d:\\Github\\Colonise\\Config\\source\\coverage.ts",
"d:\\Github\\Colonise\\Config\\source\\distribute.ts",
"d:\\Github\\Colonise\\Config\\source\\helpers.ts",
"d:\\Github\\Colonise\\Config\\source\\index.ts",
"d:\\Github\\Colonise\\Config\\source\\install.ts",
"d:\\Github\\Colonise\\Config\\source\\lint.ts",
"d:\\Github\\Colonise\\Config\\source\\semantic-release.ts",
"d:\\Github\\Colonise\\Config\\source\\test.ts",
"d:\\Github\\Colonise\\Config\\source\\variables.ts"
]
}

0 comments on commit 6c06b70

Please sign in to comment.