The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(cli): lock the app template with the pins the publish check verified (#27864)
Weiko2acbf03a263c
diff --git a/packages/twenty-cli/CONTRIBUTING.md b/packages/twenty-cli/CONTRIBUTING.mdindex c4921491f6a..d455701284c 100644--- a/packages/twenty-cli/CONTRIBUTING.md+++ b/packages/twenty-cli/CONTRIBUTING.md@@ -131,29 +131,30 @@ to the next, unpublished SDK version right after each SDK release:3. Run the `Publish npm packages` workflow in `twenty-infra` with that tag asits `ref`. Creating the tag alone does not publish the package.-`yarn npm publish` first runs `scripts/check-publish.mjs`, which needs a fresh-build and npm registry access. It fails when the executable, worker, lazy chunks-or templates are missing, when `dist` was built for another version, when one of-its CLI runs fails or times out, or when `app init` would pin versions that are-not on npm. An unreachable registry is reported separately from a missing-version. The check does not read tags: it confirms that the pinned versions are-published. Selecting the matching SDK release remains a release step.--`prepublish` then runs `scripts/generate-template-lock.mjs`. It resolves the app-template's dependencies with the repository's Yarn against the public registry,-under a 3-day minimum release age that exempts the pinned Twenty packages, and-writes `dist/app-template/yarn.lock`. Apps created with `app init` then install-exact, integrity-checked versions, including for people who enforce a minimum-release age. The lockfile keeps the template's placeholder name, and the first+`yarn npm publish` first runs `scripts/prepublish.mjs`, which needs a fresh+build and npm registry access. It checks the build first, and fails when the+executable, worker, lazy chunks or templates are missing, when `dist` was built+for another version, when one of its CLI runs fails or times out, or when+`app init` would pin versions that are not on npm. An unreachable registry is+reported separately from a missing version. The check does not read tags: it+confirms that the pinned versions are published. Selecting the matching SDK+release remains a release step.++Once the check passes, the script resolves the built app template's+dependencies, with the versions that `app init` pinned during the check, using+the repository's Yarn against the public registry, under a 3-day minimum+release age that exempts the pinned Twenty packages. It then writes+`dist/app-template/yarn.lock`. Apps created with `app init` then install exact,+integrity-checked versions, including for people who enforce a minimum release+age. The lockfile keeps the template's placeholder name, and the first`yarn install` renames that root entry.-The tests of both scripts run with `twenty-cli:test:package`. To run the scripts+The tests of both steps run with `twenty-cli:test:package`. To run the scriptbefore tagging:```bashyarn nx build twenty-cli-node packages/twenty-cli/scripts/check-publish.mjs-node packages/twenty-cli/scripts/generate-template-lock.mjs+node packages/twenty-cli/scripts/prepublish.mjs```## App integration testsdiff --git a/packages/twenty-cli/package.json b/packages/twenty-cli/package.jsonindex ccdd232ab3f..82547f79b31 100644--- a/packages/twenty-cli/package.json+++ b/packages/twenty-cli/package.json@@ -21,7 +21,7 @@],"scripts": {"build": "npx rimraf dist && npx vite build",- "prepublish": "node scripts/check-publish.mjs && node scripts/generate-template-lock.mjs"+ "prepublish": "node scripts/prepublish.mjs"},"engines": {"node": "^24.5.0"diff --git a/packages/twenty-cli/scripts/check-publish.mjs b/packages/twenty-cli/scripts/check-publish.mjsdeleted file mode 100644index d15b614bd67..00000000000--- a/packages/twenty-cli/scripts/check-publish.mjs+++ /dev/null@@ -1,28 +0,0 @@-import { fileURLToPath } from 'node:url';--import {- checkPublishReadiness,- PublishCheckError,-} from './check-publish-readiness.mjs';--try {- const { version, executable, bundleFileCount, templateFileCount, pins } =- await checkPublishReadiness({- packageDirectory: fileURLToPath(new URL('..', import.meta.url)),- });-- console.log(`twenty ${version} is ready to publish:`);- console.log(- ` ${executable}, the app worker, ${bundleFileCount} bundle files and ${templateFileCount} template files are present`,- );- console.log(- ` app init pins ${pins.map(({ name, version: pinnedVersion }) => `${name}@${pinnedVersion}`).join(', ')}, all on npm`,- );-} catch (error) {- if (!(error instanceof PublishCheckError)) {- throw error;- }-- console.error(`Publish check failed: ${error.message}`);- process.exitCode = 1;-}diff --git a/packages/twenty-cli/scripts/generate-template-lock.mjs b/packages/twenty-cli/scripts/generate-template-lock.mjsdeleted file mode 100644index 6f97c0aca7b..00000000000--- a/packages/twenty-cli/scripts/generate-template-lock.mjs+++ /dev/null@@ -1,21 +0,0 @@-import { fileURLToPath } from 'node:url';--import { generateTemplateLock, TemplateLockError } from './template-lock.mjs';--try {- const { version, firstPartyPackages, resolutionCount } =- await generateTemplateLock({- packageDirectory: fileURLToPath(new URL('..', import.meta.url)),- });-- console.log(- `Wrote dist/app-template/yarn.lock with ${resolutionCount} resolutions, pinning ${firstPartyPackages.map((name) => `${name}@${version}`).join(', ')}`,- );-} catch (error) {- if (!(error instanceof TemplateLockError)) {- throw error;- }-- console.error(`Template lockfile failed: ${error.message}`);- process.exitCode = 1;-}diff --git a/packages/twenty-cli/scripts/prepublish.mjs b/packages/twenty-cli/scripts/prepublish.mjsnew file mode 100644index 00000000000..f26e4c083df--- /dev/null+++ b/packages/twenty-cli/scripts/prepublish.mjs@@ -0,0 +1,42 @@+import { fileURLToPath } from 'node:url';++import {+ checkPublishReadiness,+ PublishCheckError,+} from './check-publish-readiness.mjs';+import { generateTemplateLock, TemplateLockError } from './template-lock.mjs';++const packageDirectory = fileURLToPath(new URL('..', import.meta.url));++try {+ const { version, executable, bundleFileCount, templateFileCount, pins } =+ await checkPublishReadiness({ packageDirectory });+ const pinList = pins+ .map(({ name, version: pinnedVersion }) => `${name}@${pinnedVersion}`)+ .join(', ');++ console.log(`twenty ${version} is ready to publish:`);+ console.log(+ ` ${executable}, the app worker, ${bundleFileCount} bundle files and ${templateFileCount} template files are present`,+ );+ console.log(` app init pins ${pinList}, all on npm`);++ const { resolutionCount } = await generateTemplateLock({+ packageDirectory,+ pins,+ });++ console.log(+ `Wrote dist/app-template/yarn.lock with ${resolutionCount} resolutions, pinning ${pinList}`,+ );+} catch (error) {+ if (error instanceof PublishCheckError) {+ console.error(`Publish check failed: ${error.message}`);+ process.exitCode = 1;+ } else if (error instanceof TemplateLockError) {+ console.error(`Template lockfile failed: ${error.message}`);+ process.exitCode = 1;+ } else {+ throw error;+ }+}diff --git a/packages/twenty-cli/scripts/template-lock.mjs b/packages/twenty-cli/scripts/template-lock.mjsindex fb0709492fa..31b89dfd3a6 100644--- a/packages/twenty-cli/scripts/template-lock.mjs+++ b/packages/twenty-cli/scripts/template-lock.mjs@@ -17,32 +17,23 @@ const fail = (message) => {const readJson = async (path) => JSON.parse(await readFile(path, 'utf8'));-export const buildTemplateManifest = ({ templateManifest, version }) => {- const firstPartyPackages = Object.entries(- templateManifest.devDependencies ?? {},- )- .filter(([, pin]) => pin === PIN_PLACEHOLDER)+export const buildTemplateManifest = ({ templateManifest, pins }) => {+ const devDependencies = {+ ...templateManifest.devDependencies,+ ...Object.fromEntries(pins.map(({ name, version }) => [name, version])),+ };+ const unpinnedPackages = Object.entries(devDependencies)+ .filter(([, version]) => version === PIN_PLACEHOLDER).map(([name]) => name);- if (firstPartyPackages.length === 0) {- fail('The app template does not pin any first-party package.');+ if (unpinnedPackages.length > 0) {+ fail(`app init did not pin ${unpinnedPackages.join(', ')}.`);}- return {- manifest: {- ...templateManifest,- devDependencies: {- ...templateManifest.devDependencies,- ...Object.fromEntries(- firstPartyPackages.map((name) => [name, version]),- ),- },- },- firstPartyPackages,- };+ return { ...templateManifest, devDependencies };};-export const buildYarnrc = ({ registry, version, firstPartyPackages }) => {+export const buildYarnrc = ({ registry, pins }) => {const { protocol, hostname } = new URL(registry);return [@@ -55,7 +46,7 @@ export const buildYarnrc = ({ registry, version, firstPartyPackages }) => {: []),`npmMinimalAgeGate: ${RELEASE_MINIMAL_AGE_GATE}`,'npmPreapprovedPackages:',- ...firstPartyPackages.map((name) => ` - "${name}@${version}"`),+ ...pins.map(({ name, version }) => ` - "${name}@${version}"`),'',].join('\n');};@@ -133,6 +124,7 @@ const installWithYarn = async ({ yarnBinary, workingDirectory }) => {export const generateTemplateLock = async ({packageDirectory,+ pins,registry = PUBLIC_REGISTRY,install = installWithYarn,}) => {@@ -146,15 +138,9 @@ export const generateTemplateLock = async ({}const templateManifest = await readJson(- join(packageDirectory, 'app-template/package.json'),+ join(outputDirectory, 'package.json'),);- const { version } = await readJson(- join(packageDirectory, '../twenty-sdk/package.json'),- );- const { manifest, firstPartyPackages } = buildTemplateManifest({- templateManifest,- version,- });+ const manifest = buildTemplateManifest({ templateManifest, pins });const yarnBinary = await resolveYarnBinary({repositoryDirectory: join(packageDirectory, '../..'),packageManager: templateManifest.packageManager,@@ -170,14 +156,14 @@ export const generateTemplateLock = async ({);await writeFile(join(workingDirectory, '.yarnrc.yml'),- buildYarnrc({ registry, version, firstPartyPackages }),+ buildYarnrc({ registry, pins }),);try {await install({ yarnBinary, workingDirectory });} catch {fail(- `Yarn could not resolve the app template with ${firstPartyPackages.join(', ')} at ${version}. See the Yarn output above.`,+ `Yarn could not resolve the app template with ${pins.map(({ name, version }) => `${name}@${version}`).join(', ')}. See the Yarn output above.`,);}@@ -186,8 +172,8 @@ export const generateTemplateLock = async ({'utf8',);- for (const packageName of firstPartyPackages) {- assertResolvedWithIntegrity({ lockfile, packageName, version });+ for (const { name, version } of pins) {+ assertResolvedWithIntegrity({ lockfile, packageName: name, version });}if (registry === PUBLIC_REGISTRY) {@@ -197,8 +183,6 @@ export const generateTemplateLock = async ({await writeFile(join(outputDirectory, 'yarn.lock'), lockfile);return {- version,- firstPartyPackages,resolutionCount: lockfile.match(/^ {2}resolution:/gm)?.length ?? 0,};} finally {diff --git a/packages/twenty-cli/scripts/test-template-lock.mjs b/packages/twenty-cli/scripts/test-template-lock.mjsindex bfcd93a70de..012bbdb0399 100644--- a/packages/twenty-cli/scripts/test-template-lock.mjs+++ b/packages/twenty-cli/scripts/test-template-lock.mjs@@ -14,6 +14,7 @@ import {const VERSION = '2.45.0';const FIRST_PARTY_PACKAGES = ['twenty-client-sdk', 'twenty-sdk', 'twenty-ui'];+const PINS = FIRST_PARTY_PACKAGES.map((name) => ({ name, version: VERSION }));const TEMPLATE_MANIFEST = {name: 'TO-BE-GENERATED',packageManager: '[email protected]',@@ -24,6 +25,12 @@ const TEMPLATE_MANIFEST = {),},};+const PINNED_DEV_DEPENDENCIES = {+ typescript: '^5.9.3',+ 'twenty-client-sdk': VERSION,+ 'twenty-sdk': VERSION,+ 'twenty-ui': VERSION,+};const lockfileEntry = (name, { checksum = true } = {}) =>[@@ -35,6 +42,8 @@ const lockfileEntry = (name, { checksum = true } = {}) =>' linkType: hard',].join('\n');+const LOCKFILE = `${FIRST_PARTY_PACKAGES.map((name) => lockfileEntry(name)).join('\n\n')}\n`;+const createRepository = async ({yarnVersion = '4.13.0',hasBuild = true,@@ -42,25 +51,20 @@ const createRepository = async ({const root = await mkdtemp(join(tmpdir(), 'twenty-template-lock-test-'));const packageDirectory = join(root, 'packages/twenty-cli');- await mkdir(join(packageDirectory, 'app-template'), { recursive: true });- await mkdir(join(root, 'packages/twenty-sdk'), { recursive: true });+ await mkdir(packageDirectory, { recursive: true });await writeFile(join(root, '.yarnrc.yml'),`yarnPath: .yarn/releases/yarn-${yarnVersion}.cjs\n`,);- await writeFile(- join(packageDirectory, 'app-template/package.json'),- JSON.stringify(TEMPLATE_MANIFEST),- );- await writeFile(- join(root, 'packages/twenty-sdk/package.json'),- JSON.stringify({ name: 'twenty-sdk', version: VERSION }),- );if (hasBuild) {await mkdir(join(packageDirectory, 'dist/app-template'), {recursive: true,});+ await writeFile(+ join(packageDirectory, 'dist/app-template/package.json'),+ JSON.stringify(TEMPLATE_MANIFEST),+ );}return { root, packageDirectory };@@ -82,47 +86,46 @@ const testWithRepository = (name, options, run) =>}});-const assertFailure = (promise, pattern) =>- assert.rejects(promise, (error) => {- assert.ok(error instanceof TemplateLockError, error);- assert.match(error.message, pattern);+const isTemplateLockError = (pattern) => (error) => {+ assert.ok(error instanceof TemplateLockError, error);+ assert.match(error.message, pattern);- return true;- });+ return true;+};++const assertFailure = (promise, pattern) =>+ assert.rejects(promise, isTemplateLockError(pattern));-await test('pins only the first-party placeholders of the app template', () => {- const { manifest, firstPartyPackages } = buildTemplateManifest({+await test('applies the pins app init reported to the app template', () => {+ const manifest = buildTemplateManifest({templateManifest: TEMPLATE_MANIFEST,- version: VERSION,+ pins: PINS,});- assert.deepEqual(firstPartyPackages, FIRST_PARTY_PACKAGES);assert.equal(manifest.name, 'TO-BE-GENERATED');- assert.deepEqual(manifest.devDependencies, {- typescript: '^5.9.3',- 'twenty-client-sdk': VERSION,- 'twenty-sdk': VERSION,- 'twenty-ui': VERSION,- });+ assert.deepEqual(manifest.devDependencies, PINNED_DEV_DEPENDENCIES);+});++await test('refuses a template placeholder that app init did not pin', () => {+ assert.throws(+ () =>+ buildTemplateManifest({+ templateManifest: TEMPLATE_MANIFEST,+ pins: PINS.filter(({ name }) => name !== 'twenty-ui'),+ }),+ isTemplateLockError(/^app init did not pin twenty-ui\.$/),+ );});await test('resolves under an age gate that preapproves only the pinned packages', () => {- const yarnrc = buildYarnrc({- registry: PUBLIC_REGISTRY,- version: VERSION,- firstPartyPackages: FIRST_PARTY_PACKAGES,- });+ const yarnrc = buildYarnrc({ registry: PUBLIC_REGISTRY, pins: PINS });assert.match(yarnrc, /^npmMinimalAgeGate: 3d$/m);assert.match(yarnrc, /^enableScripts: false$/m);assert.match(yarnrc, /^ {2}- "twenty-sdk@2\.45\.0"$/m);assert.doesNotMatch(yarnrc, /unsafeHttpWhitelist/);assert.match(- buildYarnrc({- registry: 'http://127.0.0.1:4873',- version: VERSION,- firstPartyPackages: FIRST_PARTY_PACKAGES,- }),+ buildYarnrc({ registry: 'http://127.0.0.1:4873', pins: PINS }),/^unsafeHttpWhitelist:\n {2}- 127\.0\.0\.1$/m,);});@@ -131,26 +134,63 @@ await testWithRepository('writes the generated lockfile into the built template',{},async ({ packageDirectory }) => {- const lockfile = `${FIRST_PARTY_PACKAGES.map((name) => lockfileEntry(name)).join('\n\n')}\n`;-assert.deepEqual(await generateTemplateLock({packageDirectory,- install: writeLockfile(lockfile),+ pins: PINS,+ install: writeLockfile(LOCKFILE),}),- {- version: VERSION,- firstPartyPackages: FIRST_PARTY_PACKAGES,- resolutionCount: 3,- },+ { resolutionCount: 3 },);assert.equal(await readFile(join(packageDirectory, 'dist/app-template/yarn.lock'),'utf8',),- lockfile,+ LOCKFILE,+ );+ },+);++await testWithRepository(+ 'locks the built template with the reported pins, not the source versions',+ {},+ async ({ root, packageDirectory }) => {+ await mkdir(join(root, 'packages/twenty-sdk'), { recursive: true });+ await writeFile(+ join(root, 'packages/twenty-sdk/package.json'),+ JSON.stringify({ name: 'twenty-sdk', version: '2.46.0' }),);+ await mkdir(join(packageDirectory, 'app-template'), { recursive: true });+ await writeFile(+ join(packageDirectory, 'app-template/package.json'),+ JSON.stringify({+ ...TEMPLATE_MANIFEST,+ devDependencies: {+ ...TEMPLATE_MANIFEST.devDependencies,+ typescript: '^6.0.0',+ },+ }),+ );++ let resolvedManifest;+ let yarnrc;++ await generateTemplateLock({+ packageDirectory,+ pins: PINS,+ install: async ({ workingDirectory }) => {+ resolvedManifest = JSON.parse(+ await readFile(join(workingDirectory, 'package.json'), 'utf8'),+ );+ yarnrc = await readFile(join(workingDirectory, '.yarnrc.yml'), 'utf8');+ await writeLockfile(LOCKFILE)({ workingDirectory });+ },+ });++ assert.deepEqual(resolvedManifest.devDependencies, PINNED_DEV_DEPENDENCIES);+ assert.match(yarnrc, /^ {2}- "twenty-sdk@2\.45\.0"$/m);+ assert.doesNotMatch(yarnrc, /2\.46\.0/);},);@@ -161,6 +201,7 @@ await testWithRepository(await assertFailure(generateTemplateLock({packageDirectory,+ pins: PINS,install: writeLockfile(lockfileEntry('twenty-sdk')),}),/^twenty-client-sdk@2\.45\.0 is missing from the generated lockfile/,@@ -168,6 +209,7 @@ await testWithRepository(await assertFailure(generateTemplateLock({packageDirectory,+ pins: PINS,install: writeLockfile(FIRST_PARTY_PACKAGES.map((name) =>lockfileEntry(name, { checksum: name !== 'twenty-ui' }),@@ -186,8 +228,9 @@ await testWithRepository(await assertFailure(generateTemplateLock({packageDirectory,+ pins: PINS,install: writeLockfile(- `${FIRST_PARTY_PACKAGES.map((name) => lockfileEntry(name)).join('\n\n')}\n __archiveUrl: https://mirror.example.com\n`,+ `${LOCKFILE} __archiveUrl: https://mirror.example.com\n`,),}),/pins entries to a registry host/,@@ -202,11 +245,12 @@ await testWithRepository(await assertFailure(generateTemplateLock({packageDirectory,+ pins: PINS,install: () => {throw new Error('YN0082: No candidates found');},}),- /^Yarn could not resolve the app template with twenty-client-sdk, twenty-sdk, twenty-ui at 2\.45\.0/,+ /^Yarn could not resolve the app template with twenty-client-sdk@2\.45\.0, twenty-sdk@2\.45\.0, twenty-ui@2\.45\.0\./,);},);@@ -218,6 +262,7 @@ await testWithRepository(await assertFailure(generateTemplateLock({packageDirectory,+ pins: PINS,install: writeLockfile(''),}),/pins yarn@4\.13\.0, but the repository runs yarn 4\.12\.0/,@@ -232,6 +277,7 @@ await testWithRepository(await assertFailure(generateTemplateLock({packageDirectory,+ pins: PINS,install: writeLockfile(''),}),/^dist\/app-template is missing/,