forked from microsoft/botbuilder-js
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a04a996
commit f65e683
Showing
15 changed files
with
358 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} | ||
} |
Oops, something went wrong.