Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-joelmut committed Dec 12, 2024
1 parent a04a996 commit f65e683
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 306 deletions.
1 change: 0 additions & 1 deletion libraries/botbuilder-dialogs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"line-reader": "^0.4.0"
},
"localDependencies": {
"@microsoft/recognizers-text-choice2": "1.1.4",
"@microsoft/recognizers-text-choice": "1.1.4",
"@microsoft/recognizers-text-date-time": "1.1.4",
"@microsoft/recognizers-text-number": "1.1.4",
Expand Down
5 changes: 5 additions & 0 deletions libraries/botbuilder-repo-utils/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ export interface Package {

scripts?: Record<string, string>;
}

export interface Dependency {
name: string;
version: string;
}
3 changes: 3 additions & 0 deletions libraries/botbuilder-vendors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TODO:
- Explain how it works, now to add a new vendor, how to connect it with the rest of botbuilder libraries, etc.
- Add unit tests.
5 changes: 2 additions & 3 deletions libraries/botbuilder-vendors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
"url": "https://github.com/Microsoft/botbuilder-js.git"
},
"scripts": {
"postinstall": "ts-node src/index.ts install",
"postbuild": "ts-node src/index.ts build",
"lint": "eslint ."
"lint": "eslint .",
"connect": "ts-node src/index.ts connect"
},
"dependencies": {
"fast-glob": "^3.3.2",
Expand Down
66 changes: 66 additions & 0 deletions libraries/botbuilder-vendors/src/actions/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import path from 'path';
import { readFile, writeFile } from 'fs/promises';
import { readJsonFile } from 'botbuilder-repo-utils/src/file';
import { glob } from 'fast-glob';
import { logger } from '../utils';

export async function build({ dir, vendors, location }: any) {
if (vendors.length === 0) {
logger.package.compilation.header({ files: 0 });
return;
}

const tsconfig = await readJsonFile<any>(path.join(dir, 'tsconfig.json'));
const configDir = tsconfig.compilerOptions.outDir;
const outDir = path.resolve(dir, configDir);
const files = await glob(`**/*.js`, { cwd: outDir });

// Find and replace all vendor references in the compiled files
const references: Record<string, any> = {};
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath = path.join(outDir, file);
const content = await readFile(filePath, 'utf8');

for (const vendor of vendors) {
const vendorDir = path.join(dir, location, path.basename(vendor.dir));
const relative = path.relative(path.dirname(filePath), vendorDir).split(path.sep).join('/');
const from = `require("${vendor.name}")`;
const to = `require("${relative}")`;
if (!content.includes(from)) {
continue;
}
const line = content.split('\n').findIndex((line) => line.includes(from)) + 1;
references[file] ??= [];
references[file].push({ from, to, line });
const newContent = content.replace(from, to);
await writeFile(filePath, newContent, 'utf8');
}
}

// Log the replaced references
const entries = Object.entries(references);
logger.package.compilation.header({ files: entries.length });
for (let i = 0; i < entries.length; i++) {
const [file, refs] = entries[i];
logger.package.compilation.file.header({
isLast: i === entries.length - 1,
dir: configDir,
file,
references: refs.length,
});
for (let j = 0; j < refs.length; j++) {
const { line, from, to } = refs[j];
logger.package.compilation.file.reference({
isLast: j === refs.length - 1,
isLastParent: i === entries.length - 1,
line,
from,
to,
});
}
}
}
12 changes: 12 additions & 0 deletions libraries/botbuilder-vendors/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

export * from './build';
export * from './install';

export const actions = {
supported: ['connect'],
valid(value: string) {
return actions.supported.includes(value);
},
};
45 changes: 45 additions & 0 deletions libraries/botbuilder-vendors/src/actions/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import path from 'path';
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { copyFile, mkdir } from 'fs/promises';
import { logger } from '../utils';

export async function install({ vendors, dependencies, dir, location }: any) {
for (let i = 0; i < vendors.length; i++) {
const vendor = vendors[i];

if (!vendor.dir) {
logger.package.vendors.vendor({
isLast: i === vendors.length - 1,
name: vendor.name,
version: vendor.version,
isUnknown: true,
});
continue;
}

const source = path.join(vendor.dir, vendor.main);
const vendorDir = path.join(dir, location, path.basename(vendor.dir));
const destination = path.join(vendorDir, vendor.main);

if (!existsSync(vendorDir)) {
await mkdir(vendorDir, { recursive: true });
}

logger.package.vendors.vendor({ isLast: i === vendors.length - 1, name: vendor.name, version: vendor.version });
await copyFile(source, destination);
}

logger.package.dependencies.header({ dependencies: dependencies.length });
for (let i = 0; i < dependencies.length; i++) {
const { name, version } = dependencies[i];
logger.package.dependencies.dependency({ isLast: i === dependencies.length - 1, name, version });
if (process.env.GITHUB_ACTIONS === 'true') {
// Only modify package.json if running in GitHub Actions, preventing changes to local files and pushing them back to the repository.
execSync(`npm pkg set dependencies["${name}"]="${version}"`, { cwd: dir });
}
}
}
Loading

0 comments on commit f65e683

Please sign in to comment.