diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index fa865824b0..070ece4aa1 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - fix case sensitive import for schema entities in codegen (#2211) ### Fixed -- Glob sync issue +- Glob sync issue (#2534) ## [5.2.1] - 2024-08-12 ### Changed diff --git a/packages/cli/src/commands/build/index.ts b/packages/cli/src/commands/build/index.ts index 34f7ba6966..8342365b06 100644 --- a/packages/cli/src/commands/build/index.ts +++ b/packages/cli/src/commands/build/index.ts @@ -2,11 +2,10 @@ // SPDX-License-Identifier: GPL-3.0 import assert from 'assert'; -import {existsSync, lstatSync, readFileSync} from 'fs'; +import {existsSync, lstatSync} from 'fs'; import path from 'path'; import {Command, Flags} from '@oclif/core'; -import {globSync} from 'glob'; -import {runWebpack} from '../../controller/build-controller'; +import {getBuildEntries, runWebpack} from '../../controller/build-controller'; import {resolveToAbsolutePath, buildManifestFromLocation, getTsManifest} from '../../utils'; export default class Build extends Command { @@ -41,41 +40,9 @@ export default class Build extends Command { ); } - // Get the output location from the project package.json main field - const pjson = JSON.parse(readFileSync(path.join(directory, 'package.json')).toString()); - - const defaultEntry = path.join(directory, 'src/index.ts'); + const buildEntries = getBuildEntries(directory); const outputDir = path.resolve(directory, flags.output ?? 'dist'); - let buildEntries: Record = { - index: defaultEntry, - }; - globSync(path.join(directory, 'src/test/**/*.test.ts')).forEach((testFile) => { - const testName = path.basename(testFile).replace('.ts', ''); - buildEntries[`test/${testName}`] = testFile; - }); - - globSync(path.join(directory, 'src/tests/**/*.test.ts')).forEach((testFile) => { - const testName = path.basename(testFile).replace('.ts', ''); - buildEntries[`tests/${testName}`] = testFile; - }); - - if (pjson.exports && typeof pjson.exports !== 'string') { - buildEntries = Object.entries(pjson.exports as Record).reduce( - (acc, [key, value]) => { - acc[key] = path.resolve(directory, value); - return acc; - }, - {...buildEntries} - ); - } - - for (const i in buildEntries) { - if (typeof buildEntries[i] !== 'string') { - this.warn(`Ignoring entry ${i} from build.`); - delete buildEntries[i]; - } - } await runWebpack(buildEntries, directory, outputDir, isDev, true); if (!flags.silent) { this.log('Building and packing code ...'); diff --git a/packages/cli/src/controller/build-controller.spec.ts b/packages/cli/src/controller/build-controller.spec.ts new file mode 100644 index 0000000000..070a777a90 --- /dev/null +++ b/packages/cli/src/controller/build-controller.spec.ts @@ -0,0 +1,16 @@ +// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors +// SPDX-License-Identifier: GPL-3.0 + +import path from 'path'; +import {getBuildEntries} from './build-controller'; + +describe('build controller', () => { + it('picks up test and export files', () => { + const dir = path.resolve(__dirname, '../../test/build'); + + const entries = getBuildEntries(dir); + + expect(entries['test/mappingHandler.test']).toEqual(path.resolve(dir, './src/test/mappingHandler.test.ts')); + expect(entries.chaintypes).toEqual(path.resolve(dir, './src/chainTypes.ts')); + }); +}); diff --git a/packages/cli/src/controller/build-controller.ts b/packages/cli/src/controller/build-controller.ts index bde40e2901..5bdcd12141 100644 --- a/packages/cli/src/controller/build-controller.ts +++ b/packages/cli/src/controller/build-controller.ts @@ -2,6 +2,9 @@ // SPDX-License-Identifier: GPL-3.0 import assert from 'assert'; +import {readFileSync} from 'fs'; +import path from 'path'; +import {globSync} from 'glob'; import TerserPlugin from 'terser-webpack-plugin'; import webpack, {Configuration} from 'webpack'; import {merge} from 'webpack-merge'; @@ -92,3 +95,42 @@ export async function runWebpack( }); }); } + +export function getBuildEntries(directory: string): Record { + // FIXME: this is an assumption that the default entry is src/index.ts, in reality it should read from the project manifest + const defaultEntry = path.join(directory, 'src/index.ts'); + let buildEntries: Record = { + index: defaultEntry, + }; + + globSync(path.join(directory, 'src/test/**/*.test.ts')).forEach((testFile) => { + const testName = path.basename(testFile).replace('.ts', ''); + buildEntries[`test/${testName}`] = testFile; + }); + + globSync(path.join(directory, 'src/tests/**/*.test.ts')).forEach((testFile) => { + const testName = path.basename(testFile).replace('.ts', ''); + buildEntries[`tests/${testName}`] = testFile; + }); + + // Get the output location from the project package.json main field + const pjson = JSON.parse(readFileSync(path.join(directory, 'package.json')).toString()); + if (pjson.exports && typeof pjson.exports !== 'string') { + buildEntries = Object.entries(pjson.exports as Record).reduce( + (acc, [key, value]) => { + acc[key] = path.resolve(directory, value); + return acc; + }, + {...buildEntries} + ); + } + + for (const i in buildEntries) { + if (typeof buildEntries[i] !== 'string') { + console.warn(`Ignoring entry ${i} from build.`); + delete buildEntries[i]; + } + } + + return buildEntries; +} diff --git a/packages/cli/test/build/package.json b/packages/cli/test/build/package.json new file mode 100644 index 0000000000..2ab0cc4575 --- /dev/null +++ b/packages/cli/test/build/package.json @@ -0,0 +1,37 @@ +{ + "name": "polkadot-starter", + "version": "1.0.0", + "description": "This project can be used as a starting point for developing your SubQuery project", + "main": "dist/index.js", + "scripts": { + "build": "subql build", + "codegen": "subql codegen", + "start:docker": "docker-compose pull && docker-compose up --remove-orphans", + "dev": "subql codegen && subql build && docker-compose pull && docker-compose up --remove-orphans", + "prepack": "rm -rf dist && npm run build", + "test": "subql build && subql-node test" + }, + "homepage": "https://github.com/subquery/subql-starter", + "repository": "github:subquery/subql-starter", + "files": [ + "dist", + "schema.graphql", + "project.yaml" + ], + "author": "SubQuery Team", + "license": "MIT", + "devDependencies": { + "@polkadot/api": "^11", + "@subql/cli": "latest", + "@subql/testing": "latest", + "@subql/types": "latest", + "typescript": "^5.2.2", + "@subql/common-substrate": "^4.0.1" + }, + "resolutions": { + "ipfs-unixfs": "6.0.6" + }, + "exports": { + "chaintypes": "src/chainTypes.ts" + } +} diff --git a/packages/cli/test/build/src/chainTypes.ts b/packages/cli/test/build/src/chainTypes.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/cli/test/build/src/index.ts b/packages/cli/test/build/src/index.ts new file mode 100644 index 0000000000..df2577a74f --- /dev/null +++ b/packages/cli/test/build/src/index.ts @@ -0,0 +1,3 @@ +//Exports all handler functions +// export * from "./mappings/mappingHandlers"; +import '@polkadot/api-augment'; diff --git a/packages/cli/test/build/src/test/mappingHandler.test.ts b/packages/cli/test/build/src/test/mappingHandler.test.ts new file mode 100644 index 0000000000..ee1194dc2a --- /dev/null +++ b/packages/cli/test/build/src/test/mappingHandler.test.ts @@ -0,0 +1,11 @@ +import {subqlTest} from '@subql/testing'; + +// See https://academy.subquery.network/build/testing.html + +subqlTest( + 'handleTransfer test', // Test name + 191, // Block height to test at + [], // Dependent entities + [], // Expected entities + 'handleEvent' // handler name +);