Skip to content

Commit

Permalink
Merge pull request #738 from zapier/PDE-4678-build-in-workspaces
Browse files Browse the repository at this point in the history
PDE-4678 fix(cli): `zapier build` fails with npm workspaces
  • Loading branch information
eliangcs authored Feb 2, 2024
2 parents 18abd63 + d5f1c45 commit 3ce7c83
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 102 deletions.
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"lint": "eslint src snippets",
"lint:fix": "eslint --fix src snippets",
"test": "cross-env NODE_ENV=test mocha -t 50s --recursive src/tests --exit",
"test-debug": "cross-env NODE_ENV=test node inspect ../../node_modules/.bin/mocha -t 50s --recursive src/tests --exit",
"smoke-test": "cross-env NODE_ENV=test mocha -t 2m --recursive src/smoke-tests --exit",
"validate-templates": "./scripts/validate-app-templates.js",
"set-template-versions": "./scripts/set-app-template-versions.js",
Expand Down Expand Up @@ -59,6 +60,7 @@
"lodash": "4.17.21",
"marked": "4.2.12",
"marked-terminal": "5.2.0",
"minimatch": "9.0.3",
"node-fetch": "2.6.7",
"ora": "5.4.0",
"parse-gitignore": "0.5.1",
Expand Down
208 changes: 207 additions & 1 deletion packages/cli/src/tests/utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ describe('build (runs slowly)', function () {
runCommand('npm', ['i'], { cwd: tmpDir });
// TODO: This test depends on how "typescript" example is set up, which
// isn't good. Should refactor not to rely on that.
runCommand('npm', ['run', 'build', '--scripts-prepend-node-path'], { cwd: tmpDir });
runCommand('npm', ['run', 'build', '--scripts-prepend-node-path'], {
cwd: tmpDir,
});
entryPoint = path.resolve(tmpDir, 'index.js');
});

Expand Down Expand Up @@ -309,3 +311,207 @@ describe('build (runs slowly)', function () {
should.equal(buildExists, true);
});
});

describe('build in workspaces', function () {
let tmpDir, origCwd;

before(async () => {
tmpDir = getNewTempDirPath();

// Set up a monorepo project structure with two integrations as npm
// workspaces:
//
// (project root)
// ├─ package.json
// └── packages/
// ├─ app-1/
// │ ├─ index.js
// │ └─ package.json
// └─ app-2/
// ├─ index.js
// └─ package.json

// Create root package.json
fs.outputFileSync(
path.join(tmpDir, 'package.json'),
JSON.stringify({
name: 'my-monorepo',
workspaces: ['packages/*'],
private: true,
})
);

const defaultIndexJs = `module.exports = {
version: require('./package.json').version,
platformVersion: require('zapier-platform-core').version,
};`;

// First integration: app-1
fs.outputFileSync(
path.join(tmpDir, 'packages', 'app-1', 'index.js'),
defaultIndexJs
);
fs.outputFileSync(
path.join(tmpDir, 'packages', 'app-1', 'package.json'),
JSON.stringify({
name: 'app-1',
version: '1.0.0',
main: 'index.js',
dependencies: {
uuid: '8.3.2',
'zapier-platform-core': '15.5.1',
},
private: true,
})
);

// Second integration: app-2
fs.outputFileSync(
path.join(tmpDir, 'packages', 'app-2', 'index.js'),
defaultIndexJs
);
fs.outputFileSync(
path.join(tmpDir, 'packages', 'app-2', 'package.json'),
JSON.stringify({
name: 'app-2',
version: '1.0.0',
main: 'index.js',
dependencies: {
uuid: '9.0.1',
'zapier-platform-core': '15.5.1',
},
private: true,
})
);

runCommand('yarn', ['install'], { cwd: tmpDir });
});

after(() => {
fs.removeSync(tmpDir);
});

beforeEach(() => {
origCwd = process.cwd();
});

afterEach(() => {
process.chdir(origCwd);
});

it('should build in app-1', async () => {
const workspaceDir = path.join(tmpDir, 'packages', 'app-1');
const zipPath = path.join(workspaceDir, 'build', 'build.zip');
const unzipPath = path.join(tmpDir, 'build', 'build');

// Make sure the zapier-platform-core dependency is installed in the root
// project directory
fs.existsSync(
path.join(tmpDir, 'node_modules', 'zapier-platform-core')
).should.be.true();
fs.existsSync(
path.join(workspaceDir, 'node_modules', 'zapier-platform-core')
).should.be.false();

fs.ensureDirSync(path.dirname(zipPath));

process.chdir(workspaceDir);

await build.buildAndOrUpload(
{ build: true, upload: false },
{
skipNpmInstall: true,
skipValidation: true,
printProgress: false,
checkOutdated: false,
}
);
await decompress(zipPath, unzipPath);

const corePackageJson = JSON.parse(
fs.readFileSync(
path.join(
unzipPath,
'node_modules',
'zapier-platform-core',
'package.json'
)
)
);
corePackageJson.version.should.equal('15.5.1');

const uuidPackageJson = JSON.parse(
fs.readFileSync(
path.join(unzipPath, 'node_modules', 'uuid', 'package.json')
)
);
uuidPackageJson.version.should.equal('8.3.2');

// Make sure node_modules/app-1 and node_modules/app-2 are not included
// in the build
fs.existsSync(
path.join(unzipPath, 'node_modules', 'app-1')
).should.be.false();
fs.existsSync(
path.join(unzipPath, 'node_modules', 'app-2')
).should.be.false();
});

it('should build in app-2', async () => {
const workspaceDir = path.join(tmpDir, 'packages', 'app-2');
const zipPath = path.join(workspaceDir, 'build', 'build.zip');
const unzipPath = path.join(tmpDir, 'build', 'build');

// Make sure the zapier-platform-core dependency is installed in the root
// project directory
fs.existsSync(
path.join(tmpDir, 'node_modules', 'zapier-platform-core')
).should.be.true();
fs.existsSync(
path.join(workspaceDir, 'node_modules', 'zapier-platform-core')
).should.be.false();

fs.ensureDirSync(path.dirname(zipPath));

process.chdir(workspaceDir);

await build.buildAndOrUpload(
{ build: true, upload: false },
{
skipNpmInstall: true,
skipValidation: true,
printProgress: false,
checkOutdated: false,
}
);
await decompress(zipPath, unzipPath);

const corePackageJson = JSON.parse(
fs.readFileSync(
path.join(
unzipPath,
'node_modules',
'zapier-platform-core',
'package.json'
)
)
);
corePackageJson.version.should.equal('15.5.1');

const uuidPackageJson = JSON.parse(
fs.readFileSync(
path.join(unzipPath, 'node_modules', 'uuid', 'package.json')
)
);
uuidPackageJson.version.should.equal('9.0.1');

// Make sure node_modules/app-1 and node_modules/app-2 are not included
// in the build
fs.existsSync(
path.join(unzipPath, 'node_modules', 'app-1')
).should.be.false();
fs.existsSync(
path.join(unzipPath, 'node_modules', 'app-2')
).should.be.false();
});
});
Loading

0 comments on commit 3ce7c83

Please sign in to comment.