Skip to content

Commit 4757e20

Browse files
committed
--no-build
1 parent 7e32c17 commit 4757e20

2 files changed

Lines changed: 147 additions & 6 deletions

File tree

index.js

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ function stringifyAndNormalize(contents) {
1111
}
1212

1313
const replacers = {
14-
'package.json'(content) {
15-
return this.updatePackageJson(content);
14+
'package.json'(locals, content) {
15+
return this.updatePackageJson(locals, content);
1616
},
1717
};
1818

@@ -34,6 +34,9 @@ module.exports = {
3434
);
3535
}
3636

37+
// noBuild doesn't exist because of how ember-cli normalizes args
38+
let noBuild = options.build === false;
39+
3740
return {
3841
name: options.name,
3942
blueprintVersion: require('./package.json').version,
@@ -51,8 +54,11 @@ module.exports = {
5154
'--pnpm': isPnpm(options),
5255
'--npm': isNpm(options),
5356
'--typescript': options.typescript,
57+
'--no-build': options.noBuild,
5458
}),
5559
ciProvider: options.ciProvider,
60+
noBuild,
61+
hasBuild: !noBuild,
5662
};
5763
},
5864

@@ -77,12 +83,43 @@ module.exports = {
7783
);
7884
}
7985

86+
if (options.noBuild) {
87+
files = files.filter((filename) => {
88+
if (filename.endsWith('rollup.config.mjs')) return false;
89+
if (filename.endsWith('babel.publish.config.cjs')) return false;
90+
if (filename.endsWith('tsconfig.publish.json')) return false;
91+
92+
return true;
93+
});
94+
}
95+
8096
return files;
8197
},
8298

83-
updatePackageJson(content) {
99+
updatePackageJson(locals, content) {
84100
let contents = JSON.parse(content);
85-
return stringifyAndNormalize(sortPackageJson(contents));
101+
102+
if (locals.noBuild) {
103+
let extSuffix = locals.typescript ? 'ts' : 'js';
104+
105+
contents.exports = {
106+
'.': `./src/index.${extSuffix}`,
107+
'./addon-main.js': './addon-main.cjs',
108+
'./*': './src/*',
109+
'./*.css': './src/*.css',
110+
};
111+
112+
delete contents.scripts.build;
113+
delete contents.scripts.prepack;
114+
delete contents.devDependencies['@embroider/addon-dev'];
115+
delete contents.devDependencies['@ember/library-tsconfig'];
116+
delete contents.devDependencies['@rollup/plugin-babel'];
117+
delete contents.devDependencies['rollup'];
118+
}
119+
120+
let sorted = sortPackageJson(contents);
121+
let normalized = stringifyAndNormalize(sorted);
122+
return normalized;
86123
},
87124

88125
/**
@@ -100,11 +137,11 @@ module.exports = {
100137
* _js_eslint.config.mjs is deleted
101138
* _ts_eslint.config.mjs is renamed to eslint.config.mjs
102139
*/
103-
buildFileInfo(_intoDir, _templateVariables, file, _commandOptions) {
140+
buildFileInfo(_intoDir, locals, file, _commandOptions) {
104141
let fileInfo = this._super.buildFileInfo.apply(this, arguments);
105142

106143
if (file in replacers) {
107-
fileInfo.replacer = replacers[file].bind(this);
144+
fileInfo.replacer = replacers[file].bind(this, locals);
108145
}
109146

110147
return fileInfo;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import path, { join } from 'node:path';
2+
import tmp from 'tmp-promise';
3+
import fs from 'node:fs/promises';
4+
import fixturify from 'fixturify';
5+
import { execa } from 'execa';
6+
import { beforeAll, beforeEach, describe, expect, it } from 'vitest';
7+
8+
import {
9+
assertGeneratedCorrectly,
10+
filesMatching,
11+
SUPPORTED_PACKAGE_MANAGERS,
12+
safeExecaEnv,
13+
} from '../helpers.js';
14+
const blueprintPath = path.join(__dirname, '../..');
15+
let localEmberCli = require.resolve('ember-cli/bin/ember');
16+
17+
for (let packageManager of SUPPORTED_PACKAGE_MANAGERS) {
18+
describe(`--no-build with ${packageManager}`, () => {
19+
let tmpDir: string;
20+
let addonDir: string;
21+
let addonName = 'my-addon';
22+
23+
async function commandSucceeds(command: string) {
24+
let result = await execa({
25+
cwd: addonDir,
26+
shell: true,
27+
preferLocal: true,
28+
extendEnv: false,
29+
env: safeExecaEnv(),
30+
// Allows us to not fail yet when the command fails
31+
// but we'd still fail appropriately with the exitCode check below.
32+
// When we fail, we want to check for git diffs for debugging purposes.
33+
reject: false,
34+
})(command);
35+
36+
if (result.exitCode !== 0) {
37+
console.log(result);
38+
console.log(`\n\n${command} exited with code ${result.exitCode}\n\n`);
39+
console.log(result.stdout);
40+
console.log(result.stderr);
41+
console.log(`\n\n git diff \n\n`);
42+
await execa({ cwd: addonDir, stdio: 'inherit' })`git diff`;
43+
}
44+
45+
expect(result.exitCode, `\`${command}\` succeeds`).toEqual(0);
46+
47+
return result;
48+
}
49+
50+
beforeAll(async () => {
51+
tmpDir = (await tmp.dir()).path;
52+
addonDir = join(tmpDir, addonName);
53+
await execa({
54+
cwd: tmpDir,
55+
extendEnv: false,
56+
})`${localEmberCli} addon ${addonName} -b ${blueprintPath} --skip-npm --prefer-local true --${packageManager} --no-build`;
57+
// Have to use --force because NPM is *stricter* when you use tags in package.json
58+
// than pnpm (in that tags don't match any specified stable version)
59+
if (packageManager === 'npm') {
60+
await execa({ cwd: addonDir, extendEnv: false })`npm install --force`;
61+
} else if (packageManager === 'pnpm') {
62+
await execa({ cwd: addonDir, extendEnv: false })`pnpm install`;
63+
}
64+
});
65+
66+
it('was generated correctly', async () => {
67+
assertGeneratedCorrectly({
68+
projectRoot: addonDir,
69+
packageManager,
70+
typeScript: false,
71+
});
72+
});
73+
74+
// Tests are additive, so when running them in order, we want to check linting
75+
// before we add files from fixtures
76+
it('lints pass', async () => {
77+
await commandSucceeds(`${packageManager} run lint`);
78+
});
79+
80+
describe('with fixture', () => {
81+
beforeEach(async () => {
82+
let addonFixture = fixturify.readSync('./fixtures/typescript');
83+
fixturify.writeSync(addonDir, addonFixture);
84+
85+
// It's important that we ensure that dist directory is empty for these tests,
86+
// troll-y things can happen with shared dists
87+
await fs.rm(join(addonDir, 'dist'), { recursive: true, force: true });
88+
});
89+
90+
it('lint:fix', async () => {
91+
await commandSucceeds(`${packageManager} run lint:fix`);
92+
});
93+
94+
it('test', async () => {
95+
let testResult = await commandSucceeds(`${packageManager} run test`);
96+
97+
console.log(testResult.stdout);
98+
expect(testResult.stdout).to.include('# tests 2');
99+
expect(testResult.stdout).to.include('# pass 2');
100+
expect(testResult.stdout).to.include('# fail 0');
101+
});
102+
});
103+
});
104+
}

0 commit comments

Comments
 (0)