build: extracted custom build config to packages themselves
This commit is contained in:
parent
e0f4b0d7b5
commit
4c42304a79
8 changed files with 120 additions and 93 deletions
|
@ -226,7 +226,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
files: ['**/scripts/**', '*.spec.ts', 'packages/create-*/**'],
|
files: ['**/scripts/**', '*.spec.ts', 'packages/create-*/**', '**/build.config.cjs'],
|
||||||
rules: {
|
rules: {
|
||||||
'no-console': 'off',
|
'no-console': 'off',
|
||||||
'no-restricted-imports': 'off',
|
'no-restricted-imports': 'off',
|
||||||
|
|
17
packages/client/build.config.cjs
Normal file
17
packages/client/build.config.cjs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
module.exports = ({ fs, path, outDir }) => ({
|
||||||
|
esmOnlyDirectives: true,
|
||||||
|
final() {
|
||||||
|
function fixClient(file) {
|
||||||
|
// make TelegramClient a class, not an interface
|
||||||
|
const dTsContent = fs.readFileSync(path.join(outDir, file), 'utf8')
|
||||||
|
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(outDir, file),
|
||||||
|
dTsContent.replace('export interface TelegramClient', 'export class TelegramClient'),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fixClient('esm/client.d.ts')
|
||||||
|
fixClient('cjs/client.d.ts')
|
||||||
|
},
|
||||||
|
})
|
10
packages/core/build.config.cjs
Normal file
10
packages/core/build.config.cjs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
module.exports = ({ path, transformFile, packageDir, outDir }) => ({
|
||||||
|
esmOnlyDirectives: true,
|
||||||
|
final() {
|
||||||
|
const version = require(path.join(packageDir, 'package.json')).version
|
||||||
|
const replaceVersion = (content) => content.replace('%VERSION%', version)
|
||||||
|
|
||||||
|
transformFile(path.join(outDir, 'cjs/network/network-manager.js'), replaceVersion)
|
||||||
|
transformFile(path.join(outDir, 'esm/network/network-manager.js'), replaceVersion)
|
||||||
|
},
|
||||||
|
})
|
6
packages/create-bot/build.config.cjs
Normal file
6
packages/create-bot/build.config.cjs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module.exports = ({ fs, path, packageDir, outDir }) => ({
|
||||||
|
buildCjs: false,
|
||||||
|
final() {
|
||||||
|
fs.cpSync(path.join(packageDir, 'template'), path.join(outDir, 'template'), { recursive: true })
|
||||||
|
},
|
||||||
|
})
|
19
packages/crypto-node/build.config.cjs
Normal file
19
packages/crypto-node/build.config.cjs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
module.exports = ({ fs, path, packageDir, outDir }) => ({
|
||||||
|
final() {
|
||||||
|
// copy native sources and binding.gyp file
|
||||||
|
|
||||||
|
fs.cpSync(path.join(packageDir, 'lib'), path.join(outDir, 'lib'), { recursive: true })
|
||||||
|
|
||||||
|
const bindingGyp = fs.readFileSync(path.join(packageDir, 'binding.gyp'), 'utf8')
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(outDir, 'binding.gyp'),
|
||||||
|
bindingGyp
|
||||||
|
// replace paths to crypto
|
||||||
|
.replace(/"\.\.\/crypto/g, '"crypto'),
|
||||||
|
)
|
||||||
|
|
||||||
|
// for some unknown fucking reason ts doesn't do this
|
||||||
|
fs.copyFileSync(path.join(packageDir, 'src/native.cjs'), path.join(outDir, 'cjs/native.cjs'))
|
||||||
|
fs.copyFileSync(path.join(packageDir, 'src/native.cjs'), path.join(outDir, 'esm/native.cjs'))
|
||||||
|
},
|
||||||
|
})
|
1
packages/node/build.config.cjs
Normal file
1
packages/node/build.config.cjs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
module.exports = () => ({ esmOnlyDirectives: true })
|
26
packages/tl/build.config.cjs
Normal file
26
packages/tl/build.config.cjs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
module.exports = ({ fs, path, outDir, packageDir }) => ({
|
||||||
|
buildTs: false,
|
||||||
|
buildCjs: false,
|
||||||
|
final() {
|
||||||
|
// create package by copying all the needed files
|
||||||
|
const files = [
|
||||||
|
'binary/reader.d.ts',
|
||||||
|
'binary/reader.js',
|
||||||
|
'binary/rsa-keys.d.ts',
|
||||||
|
'binary/rsa-keys.js',
|
||||||
|
'binary/writer.d.ts',
|
||||||
|
'binary/writer.js',
|
||||||
|
'index.d.ts',
|
||||||
|
'index.js',
|
||||||
|
'raw-errors.json',
|
||||||
|
'mtp-schema.json',
|
||||||
|
'api-schema.json',
|
||||||
|
]
|
||||||
|
|
||||||
|
fs.mkdirSync(path.join(outDir, 'binary'), { recursive: true })
|
||||||
|
|
||||||
|
for (const f of files) {
|
||||||
|
fs.copyFileSync(path.join(packageDir, f), path.join(outDir, f))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
|
@ -9,86 +9,15 @@ if (process.argv.length < 3) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const packageDir = path.join(__dirname, '../packages', process.argv[2])
|
const packageDir = path.join(__dirname, '../packages', process.argv[2])
|
||||||
const BUILD_CONFIGS = {
|
const outDir = path.join(packageDir, 'dist')
|
||||||
tl: {
|
|
||||||
buildTs: false,
|
|
||||||
buildCjs: false,
|
|
||||||
customScript(packageDir, outDir) {
|
|
||||||
// create package by copying all the needed files
|
|
||||||
const files = [
|
|
||||||
'binary/reader.d.ts',
|
|
||||||
'binary/reader.js',
|
|
||||||
'binary/rsa-keys.d.ts',
|
|
||||||
'binary/rsa-keys.js',
|
|
||||||
'binary/writer.d.ts',
|
|
||||||
'binary/writer.js',
|
|
||||||
'index.d.ts',
|
|
||||||
'index.js',
|
|
||||||
'raw-errors.json',
|
|
||||||
'mtp-schema.json',
|
|
||||||
'api-schema.json',
|
|
||||||
]
|
|
||||||
|
|
||||||
fs.mkdirSync(path.join(outDir, 'binary'), { recursive: true })
|
function exec(cmd, params) {
|
||||||
|
cp.execSync(cmd, { cwd: packageDir, stdio: 'inherit', ...params })
|
||||||
|
}
|
||||||
|
|
||||||
for (const f of files) {
|
function transformFile(file, transform) {
|
||||||
fs.copyFileSync(path.join(packageDir, f), path.join(outDir, f))
|
const content = fs.readFileSync(file, 'utf8')
|
||||||
}
|
fs.writeFileSync(file, transform(content))
|
||||||
},
|
|
||||||
},
|
|
||||||
core: {
|
|
||||||
esmOnlyDirectives: true,
|
|
||||||
customScript(packageDir, outDir) {
|
|
||||||
const version = require(path.join(packageDir, 'package.json')).version
|
|
||||||
const replaceVersion = (content) => content.replace('%VERSION%', version)
|
|
||||||
|
|
||||||
transformFile(path.join(outDir, 'cjs/network/network-manager.js'), replaceVersion)
|
|
||||||
transformFile(path.join(outDir, 'esm/network/network-manager.js'), replaceVersion)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
client: {
|
|
||||||
esmOnlyDirectives: true,
|
|
||||||
customScript(packageDir, outDir) {
|
|
||||||
function fixClient(file) {
|
|
||||||
// make TelegramClient a class, not an interface
|
|
||||||
const dTsContent = fs.readFileSync(path.join(outDir, file), 'utf8')
|
|
||||||
|
|
||||||
fs.writeFileSync(
|
|
||||||
path.join(outDir, file),
|
|
||||||
dTsContent.replace('export interface TelegramClient', 'export class TelegramClient'),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fixClient('esm/client.d.ts')
|
|
||||||
fixClient('cjs/client.d.ts')
|
|
||||||
},
|
|
||||||
},
|
|
||||||
'crypto-node': {
|
|
||||||
customScript(packageDir, outDir) {
|
|
||||||
// copy native sources and binding.gyp file
|
|
||||||
|
|
||||||
fs.cpSync(path.join(packageDir, 'lib'), path.join(outDir, 'lib'), { recursive: true })
|
|
||||||
|
|
||||||
const bindingGyp = fs.readFileSync(path.join(packageDir, 'binding.gyp'), 'utf8')
|
|
||||||
fs.writeFileSync(
|
|
||||||
path.join(outDir, 'binding.gyp'),
|
|
||||||
bindingGyp
|
|
||||||
// replace paths to crypto
|
|
||||||
.replace(/"\.\.\/crypto/g, '"crypto'),
|
|
||||||
)
|
|
||||||
|
|
||||||
// for some unknown fucking reason ts doesn't do this
|
|
||||||
fs.copyFileSync(path.join(packageDir, 'src/native.cjs'), path.join(outDir, 'cjs/native.cjs'))
|
|
||||||
fs.copyFileSync(path.join(packageDir, 'src/native.cjs'), path.join(outDir, 'esm/native.cjs'))
|
|
||||||
},
|
|
||||||
},
|
|
||||||
node: { esmOnlyDirectives: true },
|
|
||||||
'create-bot': {
|
|
||||||
buildCjs: false,
|
|
||||||
customScript(packageDir, outDir) {
|
|
||||||
fs.cpSync(path.join(packageDir, 'template'), path.join(outDir, 'template'), { recursive: true })
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const buildConfig = {
|
const buildConfig = {
|
||||||
|
@ -97,10 +26,38 @@ const buildConfig = {
|
||||||
removeReferenceComments: true,
|
removeReferenceComments: true,
|
||||||
replaceSrcImports: true,
|
replaceSrcImports: true,
|
||||||
esmOnlyDirectives: false,
|
esmOnlyDirectives: false,
|
||||||
customScript: () => {},
|
before: () => {},
|
||||||
...BUILD_CONFIGS[process.argv[2]],
|
final: () => {},
|
||||||
|
...(() => {
|
||||||
|
let config
|
||||||
|
|
||||||
|
try {
|
||||||
|
config = require(path.join(packageDir, 'build.config.cjs'))
|
||||||
|
} catch (e) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[i] Using custom build config')
|
||||||
|
|
||||||
|
if (typeof config === 'function') {
|
||||||
|
config = config({
|
||||||
|
fs,
|
||||||
|
path,
|
||||||
|
exec,
|
||||||
|
transformFile,
|
||||||
|
packageDir,
|
||||||
|
outDir,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(config)
|
||||||
|
|
||||||
|
return config
|
||||||
|
})(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(buildConfig)
|
||||||
|
|
||||||
function buildPackageJson() {
|
function buildPackageJson() {
|
||||||
const pkgJson = JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json'), 'utf-8'))
|
const pkgJson = JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json'), 'utf-8'))
|
||||||
|
|
||||||
|
@ -160,21 +117,12 @@ function buildPackageJson() {
|
||||||
fs.writeFileSync(path.join(packageDir, 'dist/package.json'), JSON.stringify(pkgJson, null, 2))
|
fs.writeFileSync(path.join(packageDir, 'dist/package.json'), JSON.stringify(pkgJson, null, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
function exec(cmd) {
|
|
||||||
cp.execSync(cmd, { cwd: packageDir, stdio: 'inherit' })
|
|
||||||
}
|
|
||||||
|
|
||||||
function transformFile(file, transform) {
|
|
||||||
const content = fs.readFileSync(file, 'utf8')
|
|
||||||
fs.writeFileSync(file, transform(content))
|
|
||||||
}
|
|
||||||
|
|
||||||
const outDir = path.join(packageDir, 'dist')
|
|
||||||
|
|
||||||
// clean
|
// clean
|
||||||
fs.rmSync(path.join(outDir), { recursive: true, force: true })
|
fs.rmSync(path.join(outDir), { recursive: true, force: true })
|
||||||
fs.mkdirSync(path.join(outDir), { recursive: true })
|
fs.mkdirSync(path.join(outDir), { recursive: true })
|
||||||
|
|
||||||
|
buildConfig.before()
|
||||||
|
|
||||||
if (buildConfig.buildTs) {
|
if (buildConfig.buildTs) {
|
||||||
console.log('[i] Building typescript...')
|
console.log('[i] Building typescript...')
|
||||||
exec('pnpm exec tsc --build', { cwd: packageDir, stdio: 'inherit' })
|
exec('pnpm exec tsc --build', { cwd: packageDir, stdio: 'inherit' })
|
||||||
|
@ -262,6 +210,6 @@ fs.cpSync(path.join(__dirname, '../LICENSE'), path.join(outDir, 'LICENSE'))
|
||||||
|
|
||||||
fs.writeFileSync(path.join(outDir, '.npmignore'), '*.tsbuildinfo\n')
|
fs.writeFileSync(path.join(outDir, '.npmignore'), '*.tsbuildinfo\n')
|
||||||
|
|
||||||
buildConfig.customScript(packageDir, outDir)
|
buildConfig.final()
|
||||||
|
|
||||||
console.log('[v] Done!')
|
console.log('[v] Done!')
|
||||||
|
|
Loading…
Reference in a new issue