Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ext 7 #963

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Ext 7 #963

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/external
/lib
/build
/assets
input
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"license": "MIT",
"author": "Yandex Data UI Team <[email protected]>",
"exports": {
"./package": "./package.json"
"./package": "./package.json",
"./lib/*": "./lib/*/index.js"
},
"main": "build/index.js",
"bin": {
Expand All @@ -34,7 +35,9 @@
],
"scripts": {
"build": "npm run build:clean && node scripts/build.cli.js",
"transpile": "npm run transpile:clean && tspc -p tsconfig.transpile.json",
"build:clean": "rm -rf build assets coverage",
"transpile:clean": "rm -rf lib",
"lint": "eslint \"src/**/*.{js,jsx,ts,tsx}\"",
"lint:fix": "npm run lint -- --fix",
"prepublishOnly": "npm run lint && npm run build",
Expand Down Expand Up @@ -109,7 +112,9 @@
"tapable": "^2.2.1",
"tar-stream": "^3.1.4",
"ts-dedent": "^2.2.0",
"ts-patch": "^3.3.0",
"typescript": "^5.4.5",
"typescript-transform-paths": "^3.5.3",
"uuid": "^11.0.4",
"vite-tsconfig-paths": "^4.2.3",
"vitest": "^2.1.8",
Expand Down
21 changes: 18 additions & 3 deletions scripts/build.cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {resolve, join, dirname} = require('path');
const esbuild = require('esbuild');
const tsPaths = require('./ts-paths');
const externals = require('./external-core');
const shell = require('shelljs');

const SEARCH_API = require.resolve('@diplodoc/search-extension/worker');
Expand All @@ -24,6 +25,10 @@ const assets = [

const {version, dependencies} = require('../package.json');

const OPENAPI_EXTENSION = 'build/extensions/openapi/index.js';
const GENERIC_INCLUDER_EXTENSION = 'build/extensions/generic-includer/index.js';
const GITHUB_VCS_CONNECTOR_EXTENSION = 'build/extensions/github-vcs-connector/index.js';

const commonConfig = {
tsconfig: './tsconfig.json',
platform: 'node',
Expand All @@ -35,19 +40,28 @@ const commonConfig = {
'.map': 'empty',
},
plugins:[
tsPaths()
tsPaths(),
externals({
[resolve('src/core')]: resolve('lib')
})
],
define: {
VERSION: JSON.stringify(version),
OPENAPI_EXTENSION: JSON.stringify(resolve(OPENAPI_EXTENSION)),
GENERIC_INCLUDER_EXTENSION: JSON.stringify(resolve(GENERIC_INCLUDER_EXTENSION)),
GITHUB_VCS_CONNECTOR_EXTENSION: JSON.stringify(resolve(GITHUB_VCS_CONNECTOR_EXTENSION)),
},
};

const builds = [
[['src/extensions/openapi/index.ts'], OPENAPI_EXTENSION, ['@diplodoc/cli', '@diplodoc/openapi-extension']],
[['src/extensions/generic-includer/index.ts'], GENERIC_INCLUDER_EXTENSION, ['@diplodoc/cli']],
[['src/extensions/github-vcs-connector/index.ts'], GITHUB_VCS_CONNECTOR_EXTENSION, ['@diplodoc/cli']],
[['src/index.ts'], 'build/index.js'],
[['src/workers/linter/index.ts'], 'build/linter.js'],
];

Promise.all(builds.map(([entries, outfile]) => {
Promise.all([].concat(builds.map(([entries, outfile, externals = []]) => {
const currentConfig = {
...commonConfig,
entryPoints: entries,
Expand All @@ -63,10 +77,11 @@ Promise.all(builds.map(([entries, outfile]) => {
currentConfig.external = [
...Object.keys(dependencies),
'@diplodoc/cli/package',
...externals,
];

return esbuild.build(currentConfig);
})).then(() => {
}))).then(() => {
shell.mkdir('-p', ASSETS_PATH);
for (const file of assets) {
shell.cp('-f', join(CLIENT_PATH, file), join(ASSETS_PATH, file));
Expand Down
35 changes: 35 additions & 0 deletions scripts/external-core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const {resolve} = require('path');

module.exports = (paths) => {
const pathKeys = Object.keys(paths);

return {
name: 'externals',
setup(build) {
build.onResolve({filter: /.*/}, ({path, resolveDir}) => {
if (!isRelative(path)) {
return;
}

const fullPath = resolve(resolveDir, path);
const pathKey = pathKeys.find((pkey) => fullPath.startsWith(pkey));

if (!pathKey) {
return;
}

const pathValue = paths[pathKey];

return {
path: fullPath.replace(pathKey, pathValue),
external: true,
};
});
},
};
};


function isRelative(path) {
return /\.{1,2}\//.test(path);
}
36 changes: 17 additions & 19 deletions src/commands/build/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,18 @@ import {describe, expect, it, vi} from 'vitest';
import {when} from 'vitest-when';
import {Build} from '..';
import {Run} from '../run';
import {parse} from '~/commands/parser';
import {handler as originalHandler} from '../handler';
import {withConfigUtils} from '~/config';
import {getHooks as getBaseHooks} from '~/core/program';
import {withConfigUtils} from '~/core/config';

export const handler = originalHandler as Mock;

// eslint-disable-next-line no-var
var resolveConfig: Mock;

vi.mock('shelljs');
vi.mock('../legacy-config');
vi.mock('../handler');
vi.mock('../run', async (importOriginal) => {
return {
...((await importOriginal()) as {}),
copy: vi.fn(),
};
});
vi.mock('~/config', async (importOriginal) => {
vi.mock('~/core/config', async (importOriginal) => {
resolveConfig = vi.fn((_path, {defaults, fallback}) => {
return defaults || fallback;
});
Expand Down Expand Up @@ -88,15 +82,14 @@ type BuildState = {
globs?: Hash<NormalizedPath[]>;
files?: Hash<string>;
};
export function setupBuild(state: BuildState = {}): Build & {run: Run} {
export function setupBuild(state?: BuildState): Build & {run: Run} {
const build = new Build();

build.apply();
build.hooks.BeforeAnyRun.tap('Tests', (run) => {
(build as Build & {run: Run}).run = run;
getBaseHooks(build).BeforeAnyRun.tap('Tests', (run) => {
(build as Build & {run: Run}).run = run as Run;

if (!(run as RunSpy)[Mocked]) {
setupRun({}, run);
setupRun({}, run as Run);
}

when(run.copy).calledWith(expect.anything(), expect.anything()).thenResolve();
Expand All @@ -107,13 +100,13 @@ export function setupBuild(state: BuildState = {}): Build & {run: Run} {
when(run.glob).calledWith('**/toc.yaml', expect.anything()).thenResolve([]);
when(run.glob).calledWith('**/presets.yaml', expect.anything()).thenResolve([]);

if (state.globs) {
if (state && state.globs) {
for (const [pattern, files] of Object.entries(state.globs)) {
when(run.glob).calledWith(pattern, expect.anything()).thenResolve(files);
}
}

if (state.files) {
if (state && state.files) {
for (const [file, content] of Object.entries(state.files)) {
when(run.read).calledWith(join(run.input, file)).thenResolve(content);
}
Expand All @@ -123,9 +116,14 @@ export function setupBuild(state: BuildState = {}): Build & {run: Run} {
return build as Build & {run: Run};
}

export async function runBuild(args: string, build?: Build) {
export async function runBuild(argv: string, build?: Build) {
build = build || setupBuild();
await build.parse(['node', 'index'].concat(args.split(' ')));

const rawArgs = ['node', 'index'].concat(argv.split(' '));
const args = parse('build', rawArgs);

await build.init(args);
await build.parse(rawArgs);
}

export function testConfig(name: string, args: string, result: DeepPartial<BuildConfig>): void;
Expand Down
3 changes: 3 additions & 0 deletions src/commands/build/__tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global.OPENAPI_EXTENSION = '';
global.GENERIC_INCLUDER_EXTENSION = '';
global.GITHUB_VCS_CONNECTOR_EXTENSION = '';
10 changes: 2 additions & 8 deletions src/commands/build/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {bold, underline} from 'chalk';
import {options as globalOptions} from '~/program/config';
import {option} from '~/config';
import {options as globalOptions} from '~/commands/config';
import {option} from '~/core/config';
import {Stage} from '~/constants';

export enum OutputFormat {
Expand Down Expand Up @@ -119,11 +119,6 @@ const addSystemMeta = option({
desc: 'Should add system section variables form presets into files meta data.',
});

const buildDisabled = option({
flags: '--build-disabled',
desc: 'Disable building.',
});

export const options = {
input: globalOptions.input,
output: globalOptions.output,
Expand All @@ -143,5 +138,4 @@ export const options = {
ignore,
ignoreStage,
addSystemMeta,
buildDisabled,
};
1 change: 0 additions & 1 deletion src/commands/build/core/meta/index.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/commands/build/core/toc/includers/openapi.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/commands/build/core/vars/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/commands/build/errors/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/commands/build/features/changelogs/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {option} from '~/config';
import {option} from '~/core/config';

const changelogs = option({
flags: '--changelogs',
Expand Down
12 changes: 7 additions & 5 deletions src/commands/build/features/changelogs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {Build} from '~/commands';
import type {Command} from '~/config';
import {defined} from '~/config';
import type {Build} from '~/commands/build';
import type {Command} from '~/core/config';

import {getHooks as getBaseHooks} from '~/core/program';
import {defined} from '~/core/config';
import {options} from './config';

export type ChangelogsArgs = {
Expand All @@ -13,11 +15,11 @@ export type ChangelogsConfig = {

export class Changelogs {
apply(program: Build) {
program.hooks.Command.tap('Changelogs', (command: Command) => {
getBaseHooks(program).Command.tap('Changelogs', (command: Command) => {
command.addOption(options.changelogs);
});

program.hooks.Config.tap('Changelogs', (config, args) => {
getBaseHooks(program).Config.tap('Changelogs', (config, args) => {
config.changelogs = defined('changelogs', args, config) || false;

return config;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/build/features/contributors/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {option} from '~/config';
import {option} from '~/core/config';

const contributors = option({
flags: '--contributors',
Expand Down
11 changes: 6 additions & 5 deletions src/commands/build/features/contributors/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {Build} from '../..';
import type {Command} from '~/config';
import type {Build} from '~/commands/build';
import type {Command} from '~/core/config';
import type {VCSConnectorConfig} from '~/vcs-connector/connector-models';

import {defined} from '~/config';
import {getHooks as getBaseHooks} from '~/core/program';
import {defined} from '~/core/config';
import {options} from './config';

interface VCSConfiguration {
Expand Down Expand Up @@ -39,12 +40,12 @@ export type ContributorsConfig = {

export class Contributors {
apply(program: Build) {
program.hooks.Command.tap('Contributors', (command: Command) => {
getBaseHooks(program).Command.tap('Contributors', (command: Command) => {
command.addOption(options.contributors);
command.addOption(options.ignoreAuthorPatterns);
});

program.hooks.Config.tap('Contributors', (config, args) => {
getBaseHooks(program).Config.tap('Contributors', (config, args) => {
config.vcs = defined('vcs', config) || {};
config.contributors = defined('contributors', args, config) || false;
config.ignoreAuthorPatterns = defined('ignoreAuthorPatterns', args, config) || [];
Expand Down
Loading
Loading