@syncbridge/syncbuild 0.5.4 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,7 +26,7 @@ export async function buildSources(context) {
26
26
  entryPoints: [entryPoint],
27
27
  bundle: true,
28
28
  external: ['@syncbridge/common', ...Object.keys(context.externals || {})],
29
- outfile: path.join(context.outPackageDir, context.outFile),
29
+ outfile: path.join(context.tmpDir, context.outEntryPoint),
30
30
  platform: 'node',
31
31
  target: ['node22'],
32
32
  format: 'esm',
@@ -46,6 +46,11 @@ export async function buildSources(context) {
46
46
  if (context.tsconfig) {
47
47
  esbuildConfig.plugins?.push(esbuildTsc({
48
48
  tsconfig: context.tsconfig,
49
+ overwriteConfig: cfg => {
50
+ cfg.compilerOptions = cfg.compilerOptions || {};
51
+ cfg.compilerOptions.declaration = true;
52
+ cfg.declarationDir = path.join(context.tmpDir, 'typings');
53
+ },
49
54
  }));
50
55
  }
51
56
  try {
@@ -6,39 +6,30 @@ import * as jsonc from 'jsonc-parser';
6
6
  import { version } from '../constants.js';
7
7
  import { scanNativeDeps } from './utils/scan-native-deps.js';
8
8
  export function createContext(config) {
9
+ const packageDir = path.resolve(process.cwd(), config.packageDir || './');
9
10
  let pkgJson;
10
11
  try {
11
- pkgJson = jsonc.parse(fs.readFileSync(path.resolve(config.packageDir, 'package.json'), 'utf-8'));
12
+ pkgJson = jsonc.parse(fs.readFileSync(path.resolve(packageDir, 'package.json'), 'utf-8'));
12
13
  }
13
14
  catch {
14
15
  throw new SbError('package.json not found');
15
16
  }
16
17
  const context = {
18
+ logger: config.logger,
17
19
  config,
18
20
  pkgJson,
19
21
  outPkgJson: deepClone(pkgJson),
20
22
  cwd: process.cwd(),
21
- packageDir: path.resolve(config.packageDir),
22
- outDir: path.resolve(process.cwd(), config.outDir ?? 'dist'),
23
- outPackageDir: '',
24
- outTypingsDir: '',
25
- outFile: 'index.js',
23
+ packageDir,
24
+ outDir: path.resolve(packageDir, config.outDir ?? './'),
25
+ tmpDir: config.tempDir || fs.mkdtempSync(path.join(packageDir, 'tmp-')),
26
+ outEntryPoint: 'index.js',
26
27
  entryPoint: pkgJson.module ?? pkgJson.main ?? 'index.js',
27
- packageZip: '',
28
- typingsZip: '',
29
- };
30
- context.outPackageDir = path.resolve(context.outDir, 'package');
31
- context.outTypingsDir = path.resolve(context.outDir, 'typing');
32
- context.packageZip =
33
- pkgJson.name.replace(/^@/, '').replace(/\//g, '-') +
28
+ outFile: pkgJson.name.replace(/^@/, '').replace(/\//g, '-') +
34
29
  '-' +
35
- pkgJson.version +
36
- '.zip';
37
- context.typingsZip =
38
- pkgJson.name.replace(/^@/, '').replace(/\//g, '-') +
39
- '-typings-' +
40
- pkgJson.version +
41
- '.zip';
30
+ pkgJson.version,
31
+ };
32
+ fs.rmSync(context.tmpDir, { recursive: true, force: true });
42
33
  const allDeps = {
43
34
  ...pkgJson.dependencies,
44
35
  ...pkgJson.optionalDependencies,
@@ -57,7 +48,7 @@ export function createContext(config) {
57
48
  delete outPkgJson.exports;
58
49
  outPkgJson.private = true;
59
50
  outPkgJson.type = 'module';
60
- outPkgJson.module = context.outFile;
51
+ outPkgJson.module = context.outEntryPoint;
61
52
  outPkgJson.syncbridge = {
62
53
  version: /^(\d+)/.exec(version)[1],
63
54
  };
@@ -1,18 +1,20 @@
1
1
  import 'reflect-metadata';
2
2
  import fs from 'node:fs';
3
+ import { createRequire } from 'node:module';
3
4
  import path from 'node:path';
4
5
  import { COMPONENT_OPTIONS, PROCESSOR_OPTIONS } from '@syncbridge/common';
5
6
  import { resolvePromisesDeep } from './utils/resolve-promises.js';
6
7
  export async function extractExports(context) {
7
8
  const { outPkgJson } = context;
8
- const pkgGlobal = await import(path.join(context.outPackageDir, context.outFile));
9
+ const require = createRequire(path.join(context.tmpDir, 'index.js'));
10
+ const pkgGlobal = await require('./' + context.outEntryPoint);
9
11
  for (const [k, v] of Object.entries(pkgGlobal)) {
10
12
  if (typeof v !== 'function')
11
13
  continue;
12
14
  let metadata = Reflect.getMetadata(COMPONENT_OPTIONS, v);
13
15
  if (metadata) {
14
16
  metadata = await resolvePromisesDeep(metadata);
15
- const metadataDir = path.join(context.outPackageDir, 'metadata');
17
+ const metadataDir = path.join(context.tmpDir, 'metadata');
16
18
  fs.mkdirSync(metadataDir, { recursive: true });
17
19
  fs.writeFileSync(path.join(metadataDir, `${k}.json`), JSON.stringify(metadata, undefined, 2));
18
20
  outPkgJson.syncbridge.components =
@@ -21,7 +23,7 @@ export async function extractExports(context) {
21
23
  }
22
24
  metadata = Reflect.getMetadata(PROCESSOR_OPTIONS, v);
23
25
  if (metadata) {
24
- const metadataDir = path.join(context.outPackageDir, 'metadata');
26
+ const metadataDir = path.join(context.tmpDir, 'metadata');
25
27
  fs.mkdirSync(metadataDir, { recursive: true });
26
28
  fs.writeFileSync(path.join(metadataDir, `${k}.json`), JSON.stringify(metadata, undefined, 2));
27
29
  outPkgJson.syncbridge.processors =
@@ -9,7 +9,7 @@ export function readTsconfig(context) {
9
9
  overwriteConfig: cfg => {
10
10
  cfg.compilerOptions = cfg.compilerOptions || {};
11
11
  cfg.compilerOptions.sourceMap = false;
12
- cfg.compilerOptions.declarationDir = context.outTypingsDir;
12
+ cfg.compilerOptions.declarationDir = path.join(context.tmpDir, 'typings');
13
13
  },
14
14
  });
15
15
  const dtsRoot = path.join(context.packageDir, context.entryPoint.replace(/\.ts$/, '.d.ts'));
@@ -34,7 +34,7 @@ function esbuildTsc(options) {
34
34
  });
35
35
  const declarationDir = parsedTsConfig.options.declarationDir ||
36
36
  parsedTsConfig.options.outDir;
37
- if (declarationDir && parsedTsConfig.options.declaration) {
37
+ if (declarationDir && (parsedTsConfig.options.declaration ?? true)) {
38
38
  const declaration = typescript.transpileDeclaration(fileContent, {
39
39
  compilerOptions: parsedTsConfig.options,
40
40
  fileName: path.basename(args.path),
@@ -1,2 +1,2 @@
1
1
  export declare function scanNativeDeps(rootPath: string, pkgNames: string[]): Record<string, string>;
2
- export declare function _scanNativeDeps(rootPath: string, pkgName: string, externals: Record<string, string>, visited: Set<string>, ignoreNotFount?: boolean): void;
2
+ export declare function _scanNativeDeps(parentPath: string, pkgName: string, externals: Record<string, string>, visited: Set<string>, ignoreNotFount?: boolean): void;
@@ -1,6 +1,6 @@
1
1
  import fs from 'node:fs';
2
+ import { createRequire } from 'node:module';
2
3
  import path from 'node:path';
3
- import { fileURLToPath, pathToFileURL } from 'node:url';
4
4
  import { SbError } from '@syncbridge/common';
5
5
  import glob from 'fast-glob';
6
6
  import * as jsonc from 'jsonc-parser';
@@ -10,18 +10,19 @@ export function scanNativeDeps(rootPath, pkgNames) {
10
10
  pkgNames.forEach(pkgName => _scanNativeDeps(rootPath, pkgName, externals, visited));
11
11
  return externals;
12
12
  }
13
- export function _scanNativeDeps(rootPath, pkgName, externals, visited, ignoreNotFount) {
13
+ export function _scanNativeDeps(parentPath, pkgName, externals, visited, ignoreNotFount) {
14
14
  if (visited.has(pkgName))
15
15
  return;
16
16
  visited.add(pkgName);
17
+ const require = createRequire(path.join(parentPath, 'package.json'));
17
18
  let pkgDir;
18
19
  let pkgJson;
19
20
  try {
20
21
  try {
21
- const location = import.meta.resolve(pkgName, pathToFileURL(rootPath));
22
+ const location = require.resolve(pkgName);
22
23
  if (location === pkgName || location.startsWith('node:'))
23
24
  return;
24
- pkgDir = path.dirname(fileURLToPath(location));
25
+ pkgDir = path.dirname(location);
25
26
  }
26
27
  catch (e) {
27
28
  if (e.code === 'ERR_INVALID_URL_SCHEME')
@@ -60,12 +61,12 @@ export function _scanNativeDeps(rootPath, pkgName, externals, visited, ignoreNot
60
61
  }
61
62
  if (pkgJson.dependencies) {
62
63
  for (const dep of Object.keys(pkgJson.dependencies)) {
63
- _scanNativeDeps(rootPath, dep, externals, visited);
64
+ _scanNativeDeps(parentPath, dep, externals, visited);
64
65
  }
65
66
  }
66
67
  if (pkgJson.optionalDependencies) {
67
68
  for (const dep of Object.keys(pkgJson.optionalDependencies)) {
68
- _scanNativeDeps(rootPath, dep, externals, visited, true);
69
+ _scanNativeDeps(parentPath, dep, externals, visited, true);
69
70
  }
70
71
  }
71
72
  }
package/bundle-package.js CHANGED
@@ -1,27 +1,36 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
+ import AdmZip from 'adm-zip';
4
+ import colors from 'ansi-colors';
3
5
  import { rimraf } from 'rimraf';
4
6
  import { buildSources } from './builder/build-sources.js';
5
7
  import { createContext } from './builder/create-context.js';
6
8
  import { extractExports } from './builder/extract-exports.js';
7
9
  import { readTsconfig } from './builder/read-tsconfig.js';
8
- import { zipPackage } from './builder/zip-package.js';
9
10
  export async function bundlePackage(config) {
10
11
  const context = createContext(config);
11
- rimraf.sync(context.outDir);
12
- readTsconfig(context);
13
- await buildSources(context);
14
- await extractExports(context);
15
- fs.writeFileSync(path.join(context.outPackageDir, 'package.json'), JSON.stringify({
16
- ...context.outPkgJson,
17
- types: undefined,
18
- }, null, 2));
19
- fs.writeFileSync(path.join(context.outTypingsDir, 'package.json'), JSON.stringify({
20
- ...context.outPkgJson,
21
- module: '',
22
- syncbridge: undefined,
23
- }, null, 2));
24
- zipPackage(context);
25
- // rimraf.sync(context.outPackageDir);
26
- // rimraf.sync(context.outTypingsDir);
12
+ context.logger?.(`Bundling package ${colors.magenta(context.pkgJson.name)}...`);
13
+ try {
14
+ rimraf.sync(context.outDir);
15
+ readTsconfig(context);
16
+ await buildSources(context);
17
+ await extractExports(context);
18
+ fs.writeFileSync(path.join(context.tmpDir, 'package.json'), JSON.stringify({
19
+ ...context.outPkgJson,
20
+ types: './' +
21
+ path.join(`./typings`, context.entryPoint.replace(/\.js$/, '.d.ts')),
22
+ }, null, 2));
23
+ const zipFileName = path.join(context.outDir, context.outFile + '.zip');
24
+ if (fs.existsSync(context.tmpDir)) {
25
+ const zip = new AdmZip();
26
+ zip.addLocalFolder(context.tmpDir);
27
+ zip.writeZip(zipFileName);
28
+ }
29
+ context.logger?.(`Generated ${colors.magenta(path.basename(zipFileName))}`);
30
+ }
31
+ finally {
32
+ fs.rmSync(context.tmpDir, {
33
+ recursive: true,
34
+ });
35
+ }
27
36
  }
package/constants.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import process from 'node:process';
2
- export const version = '0.5.4';
2
+ export const version = '0.5.6';
3
3
  // Environment variables
4
4
  export const NODE_ENV = process.env.NODE_ENV === 'test'
5
5
  ? 'test'
package/context.d.ts CHANGED
@@ -3,15 +3,14 @@ export declare class Context {
3
3
  pkgJson: PackageJson;
4
4
  outPkgJson: PackageJson;
5
5
  packageDir: string;
6
+ tmpDir: string;
7
+ outEntryPoint: string;
6
8
  outDir: string;
7
- outPackageDir: string;
8
- outTypingsDir: string;
9
9
  outFile: string;
10
10
  config: PackageBuildConfig;
11
11
  cwd: string;
12
12
  entryPoint: string;
13
13
  tsconfig?: any;
14
14
  externals?: Record<string, string>;
15
- packageZip: string;
16
- typingsZip: string;
15
+ logger?: typeof console.log;
17
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncbridge/syncbuild",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "description": "SyncBridge Extension Package Builder",
5
5
  "author": "Panates Inc",
6
6
  "license": "MIT",
@@ -20,7 +20,7 @@
20
20
  "typescript": "^5.9.3"
21
21
  },
22
22
  "peerDependencies": {
23
- "@syncbridge/common": "^0.5.4"
23
+ "@syncbridge/common": "^0.5.6"
24
24
  },
25
25
  "type": "module",
26
26
  "module": "./index.js",
package/syncbuild-cli.js CHANGED
@@ -7,16 +7,21 @@ import { bundlePackage } from './bundle-package.js';
7
7
  import { version } from './constants.js';
8
8
  program
9
9
  .version(version)
10
- .option('-p, --path <packagePath>', 'Directory of package')
11
- .option('-o, --out <fileName>', 'Output package file name')
10
+ .option('-o, --out <directory>', 'Output directory', 'dist')
11
+ .option('-p, --package-dir <directory>', 'Directory of package')
12
+ .option('--temp-dir <directory>', 'Directory for temp files')
12
13
  .option('--tsconfig <fileName>', 'tsconfig file name', 'tsconfig.json')
13
14
  .option('--no-color', 'Disables colors in logs messages')
14
15
  .action(async (options) => {
15
16
  if (!options.color)
16
17
  colors.enabled = false;
18
+ console.log(colors.yellow('**** SyncBuild v' + version + ' ****'));
17
19
  bundlePackage({
18
- packageDir: options.path,
20
+ packageDir: options.packageDir,
21
+ outDir: options.out,
19
22
  tsconfig: options.tsconfig,
23
+ tempDir: options.tempDir,
24
+ logger: console.log,
20
25
  }).catch(e => {
21
26
  if (e instanceof SbError)
22
27
  console.error(colors.red(e.message));
package/types.d.ts CHANGED
@@ -7,6 +7,10 @@ export interface PackageBuildConfig {
7
7
  * Represents the output directory of the build.
8
8
  */
9
9
  outDir?: string;
10
+ /**
11
+ * Represents the directory to store temporary files.
12
+ */
13
+ tempDir?: string;
10
14
  /**
11
15
  * Represents the path to the tsconfig file.
12
16
  */
@@ -44,6 +48,10 @@ export interface PackageBuildConfig {
44
48
  * Default: false
45
49
  */
46
50
  keepNames?: boolean;
51
+ /**
52
+ * Custom logger function for output messages.
53
+ */
54
+ logger?: typeof console.log;
47
55
  }
48
56
  export interface PackageJson {
49
57
  name: string;
@@ -1,2 +0,0 @@
1
- import { Context } from '../context.js';
2
- export declare function zipPackage(context: Context): void;
@@ -1,15 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import AdmZip from 'adm-zip';
4
- export function zipPackage(context) {
5
- if (fs.existsSync(context.outPackageDir)) {
6
- const zip = new AdmZip();
7
- zip.addLocalFolder(context.outPackageDir);
8
- zip.writeZip(path.join(context.outDir, context.packageZip));
9
- }
10
- if (fs.existsSync(context.outTypingsDir)) {
11
- const zip = new AdmZip();
12
- zip.addLocalFolder(context.outTypingsDir);
13
- zip.writeZip(path.join(context.outDir, context.typingsZip));
14
- }
15
- }