Skip to content

Commit

Permalink
Merge pull request #16728 from Marklb/marklb/fix-run-compodoc-on-windows
Browse files Browse the repository at this point in the history
Angular: Fix runCompodoc for Windows, local Compodoc, and user specified tsconfig
  • Loading branch information
shilman committed Jan 28, 2022
1 parent 6d54cac commit c46203a
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 44 deletions.
18 changes: 10 additions & 8 deletions app/angular/src/builders/build-storybook/index.spec.ts
Expand Up @@ -11,6 +11,7 @@ const cpSpawnMock = {
spawn: jest.fn(),
};
jest.doMock('child_process', () => cpSpawnMock);

describe('Build Storybook Builder', () => {
let architect: Architect;
let architectHost: TestingArchitectHost;
Expand Down Expand Up @@ -142,14 +143,15 @@ describe('Build Storybook Builder', () => {
await run.stop();

expect(output.success).toBeTruthy();
expect(cpSpawnMock.spawn).toHaveBeenCalledWith('compodoc', [
'-p',
'./storybook/tsconfig.ts',
'-d',
'',
'-e',
'json',
]);
expect(cpSpawnMock.spawn).toHaveBeenCalledWith(
'npx',
['compodoc', '-p', './storybook/tsconfig.ts', '-d', '', '-e', 'json'],
{
cwd: '',
env: process.env,
shell: true,
}
);
expect(buildStandaloneMock).toHaveBeenCalledWith({
angularBrowserTarget: 'angular-cli:build-2',
angularBuilderContext: expect.any(Object),
Expand Down
17 changes: 9 additions & 8 deletions app/angular/src/builders/start-storybook/index.spec.ts
Expand Up @@ -154,14 +154,15 @@ describe('Start Storybook Builder', () => {
await run.stop();

expect(output.success).toBeTruthy();
expect(cpSpawnMock.spawn).toHaveBeenCalledWith('compodoc', [
'-p',
'./storybook/tsconfig.ts',
'-d',
'',
'-e',
'json',
]);
expect(cpSpawnMock.spawn).toHaveBeenCalledWith(
'npx',
['compodoc', '-p', './storybook/tsconfig.ts', '-d', '', '-e', 'json'],
{
cwd: '',
env: process.env,
shell: true,
}
);
expect(buildStandaloneMock).toHaveBeenCalledWith({
angularBrowserTarget: 'angular-cli:build-2',
angularBuilderContext: expect.any(Object),
Expand Down
88 changes: 88 additions & 0 deletions app/angular/src/builders/utils/run-compodoc.spec.ts
@@ -0,0 +1,88 @@
import { BuilderContext } from '@angular-devkit/architect';
import { LoggerApi } from '@angular-devkit/core/src/logger';
import { take } from 'rxjs/operators';

const cpSpawnMock = {
spawn: jest.fn(),
};
jest.doMock('child_process', () => cpSpawnMock);

const { runCompodoc } = require('./run-compodoc');

const builderContextLoggerMock: LoggerApi = {
createChild: jest.fn(),
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
fatal: jest.fn(),
};

describe('runCompodoc', () => {
const originalEnv = process.env;

beforeEach(() => {
process.env = { FOO: 'bar' };
cpSpawnMock.spawn.mockImplementation(() => ({
stdout: { on: () => {} },
stderr: { on: () => {} },
on: (_event: string, cb: any) => cb(0),
}));
});

afterEach(() => {
process.env = originalEnv;
jest.clearAllMocks();
});

it('should run compodoc with tsconfig from context', async () => {
runCompodoc(
{
compodocArgs: [],
tsconfig: 'path/to/tsconfig.json',
},
{
workspaceRoot: 'path/to/project',
logger: builderContextLoggerMock,
} as BuilderContext
)
.pipe(take(1))
.subscribe();

expect(cpSpawnMock.spawn).toHaveBeenCalledWith(
'npx',
['compodoc', '-p', 'path/to/tsconfig.json', '-d', 'path/to/project'],
{
cwd: 'path/to/project',
env: { FOO: 'bar' },
shell: true,
}
);
});

it('should run compodoc with tsconfig from compodocArgs', async () => {
runCompodoc(
{
compodocArgs: ['-p', 'path/to/tsconfig.stories.json'],
tsconfig: 'path/to/tsconfig.json',
},
{
workspaceRoot: 'path/to/project',
logger: builderContextLoggerMock,
} as BuilderContext
)
.pipe(take(1))
.subscribe();

expect(cpSpawnMock.spawn).toHaveBeenCalledWith(
'npx',
['compodoc', '-d', 'path/to/project', '-p', 'path/to/tsconfig.stories.json'],
{
cwd: 'path/to/project',
env: { FOO: 'bar' },
shell: true,
}
);
});
});
13 changes: 10 additions & 3 deletions app/angular/src/builders/utils/run-compodoc.ts
Expand Up @@ -2,22 +2,29 @@ import { BuilderContext } from '@angular-devkit/architect';
import { spawn } from 'child_process';
import { Observable } from 'rxjs';

const hasTsConfigArg = (args: string[]) => args.indexOf('-p') !== -1;

export const runCompodoc = (
{ compodocArgs, tsconfig }: { compodocArgs: string[]; tsconfig: string },
context: BuilderContext
): Observable<void> => {
return new Observable<void>((observer) => {
const finalCompodocArgs = [
'compodoc',
// Default options
'-p',
tsconfig,
...(hasTsConfigArg(compodocArgs) ? [] : ['-p', tsconfig]),
'-d',
`${context.workspaceRoot}`,
...compodocArgs,
];

try {
const child = spawn('compodoc', finalCompodocArgs);
context.logger.info(finalCompodocArgs.join(' '));
const child = spawn('npx', finalCompodocArgs, {
cwd: context.workspaceRoot,
env: process.env,
shell: true,
});

child.stdout.on('data', (data) => {
context.logger.info(data.toString());
Expand Down
6 changes: 2 additions & 4 deletions examples/angular-cli/angular.json
Expand Up @@ -79,15 +79,13 @@
"builder": "@storybook/angular:start-storybook",
"options": {
"browserTarget": "angular-cli:build",
"port": 4400,
"staticDir": ["src/assets"]
"port": 4400
}
},
"build-storybook": {
"builder": "@storybook/angular:build-storybook",
"options": {
"browserTarget": "angular-cli:build",
"staticDir": ["src/assets"]
"browserTarget": "angular-cli:build"
}
}
}
Expand Down
56 changes: 35 additions & 21 deletions yarn.lock
Expand Up @@ -719,25 +719,25 @@ __metadata:
linkType: hard

"@babel/core@npm:^7.13.16, @babel/core@npm:^7.16.7":
version: 7.16.7
resolution: "@babel/core@npm:7.16.7"
version: 7.16.12
resolution: "@babel/core@npm:7.16.12"
dependencies:
"@babel/code-frame": ^7.16.7
"@babel/generator": ^7.16.7
"@babel/generator": ^7.16.8
"@babel/helper-compilation-targets": ^7.16.7
"@babel/helper-module-transforms": ^7.16.7
"@babel/helpers": ^7.16.7
"@babel/parser": ^7.16.7
"@babel/parser": ^7.16.12
"@babel/template": ^7.16.7
"@babel/traverse": ^7.16.7
"@babel/types": ^7.16.7
"@babel/traverse": ^7.16.10
"@babel/types": ^7.16.8
convert-source-map: ^1.7.0
debug: ^4.1.0
gensync: ^1.0.0-beta.2
json5: ^2.1.2
semver: ^6.3.0
source-map: ^0.5.0
checksum: 01e69670445a712fba0c6a2d20b442fac6c7bd48d3bee25f3034a5a772bdf7fe442f1164d5c0d4ad420aacf19d701326d014e9bb0daa88fdd0831d0cc832e7fd
checksum: 3e62056eb6e9e20dc785001d720f7958d4e407e5d3ad6203471f85482381c59b5fd9237601be10941aa47394a3bbba2679e7c5850c31225bbae3aa0db45009bf
languageName: node
linkType: hard

Expand Down Expand Up @@ -785,7 +785,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/generator@npm:^7.16.7, @babel/generator@npm:^7.16.8, @babel/generator@npm:^7.8.7":
"@babel/generator@npm:^7.16.8, @babel/generator@npm:^7.8.7":
version: 7.16.8
resolution: "@babel/generator@npm:7.16.8"
dependencies:
Expand Down Expand Up @@ -1581,7 +1581,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/parser@npm:^7.16.10, @babel/parser@npm:^7.8.6, @babel/parser@npm:^7.8.7":
"@babel/parser@npm:^7.16.10, @babel/parser@npm:^7.16.12, @babel/parser@npm:^7.8.6, @babel/parser@npm:^7.8.7":
version: 7.16.12
resolution: "@babel/parser@npm:7.16.12"
bin:
Expand Down Expand Up @@ -4209,39 +4209,39 @@ __metadata:
languageName: node
linkType: hard

"@babel/traverse@npm:^7.16.7, @babel/traverse@npm:^7.16.8":
version: 7.16.8
resolution: "@babel/traverse@npm:7.16.8"
"@babel/traverse@npm:^7.16.10, @babel/traverse@npm:^7.8.6":
version: 7.16.10
resolution: "@babel/traverse@npm:7.16.10"
dependencies:
"@babel/code-frame": ^7.16.7
"@babel/generator": ^7.16.8
"@babel/helper-environment-visitor": ^7.16.7
"@babel/helper-function-name": ^7.16.7
"@babel/helper-hoist-variables": ^7.16.7
"@babel/helper-split-export-declaration": ^7.16.7
"@babel/parser": ^7.16.8
"@babel/parser": ^7.16.10
"@babel/types": ^7.16.8
debug: ^4.1.0
globals: ^11.1.0
checksum: ee8e9dd51143152ee02865a039423d1543966dcfc519762c3eb31700ceb5773ddbc516a7f84ac5eee4f2cd81099f206f0b7f35ddee183c68b51e4232fdc0363a
checksum: f0c1bac0f037f72a64628f768d1343326bc27facec2fa22b2c855400481faac3e6f4bbefc878c995e60885f6ca4580af56ab15ef147a3ba9d17c871e9804b019
languageName: node
linkType: hard

"@babel/traverse@npm:^7.8.6":
version: 7.16.10
resolution: "@babel/traverse@npm:7.16.10"
"@babel/traverse@npm:^7.16.7, @babel/traverse@npm:^7.16.8":
version: 7.16.8
resolution: "@babel/traverse@npm:7.16.8"
dependencies:
"@babel/code-frame": ^7.16.7
"@babel/generator": ^7.16.8
"@babel/helper-environment-visitor": ^7.16.7
"@babel/helper-function-name": ^7.16.7
"@babel/helper-hoist-variables": ^7.16.7
"@babel/helper-split-export-declaration": ^7.16.7
"@babel/parser": ^7.16.10
"@babel/parser": ^7.16.8
"@babel/types": ^7.16.8
debug: ^4.1.0
globals: ^11.1.0
checksum: f0c1bac0f037f72a64628f768d1343326bc27facec2fa22b2c855400481faac3e6f4bbefc878c995e60885f6ca4580af56ab15ef147a3ba9d17c871e9804b019
checksum: ee8e9dd51143152ee02865a039423d1543966dcfc519762c3eb31700ceb5773ddbc516a7f84ac5eee4f2cd81099f206f0b7f35ddee183c68b51e4232fdc0363a
languageName: node
linkType: hard

Expand Down Expand Up @@ -16947,13 +16947,20 @@ __metadata:
languageName: node
linkType: hard

"caniuse-lite@npm:^1.0.30001280, caniuse-lite@npm:^1.0.30001286":
"caniuse-lite@npm:^1.0.30001280":
version: 1.0.30001300
resolution: "caniuse-lite@npm:1.0.30001300"
checksum: c168270b3b383f2b7ea18284ad40df47c7badc30ff47ea4c8d0fc35d30989935b35d27fbff4ab6c226dcc5bedaeed38057455824aebff5260d53f784f0f4b591
languageName: node
linkType: hard

"caniuse-lite@npm:^1.0.30001286":
version: 1.0.30001292
resolution: "caniuse-lite@npm:1.0.30001292"
checksum: c9b106dee532c0ffad22bb7e7967f38b60f386c7282fdabe141d7e1feecb361eeb3940ceb33c4f773b4483f28ddab82904d4fa2ed3425babf227f5c08348cfd0
languageName: node
linkType: hard

"canonical-path@npm:1.0.0":
version: 1.0.0
resolution: "canonical-path@npm:1.0.0"
Expand Down Expand Up @@ -20809,13 +20816,20 @@ __metadata:
languageName: node
linkType: hard

"electron-to-chromium@npm:^1.3.896, electron-to-chromium@npm:^1.4.17":
"electron-to-chromium@npm:^1.3.896":
version: 1.4.49
resolution: "electron-to-chromium@npm:1.4.49"
checksum: 2773ecc57521e2a47d355da08b3b051ef1ce5322e5e4075b67a5ee3c8ed30cc99f2f69a7171a931caf90334720c33b5097f4da18f85004b6058790c327b8f4a6
languageName: node
linkType: hard

"electron-to-chromium@npm:^1.4.17":
version: 1.4.27
resolution: "electron-to-chromium@npm:1.4.27"
checksum: 5107acf4d321f80c2560df71d7dd23261d114a64554f0308a04a1981f5697e8c892f594472c0bf1681c87ac603239efc264e6193eb40cc221d1c3796ef6ab8c4
languageName: node
linkType: hard

"element-resize-detector@npm:^1.2.2":
version: 1.2.2
resolution: "element-resize-detector@npm:1.2.2"
Expand Down

0 comments on commit c46203a

Please sign in to comment.