Skip to content

Commit

Permalink
Non-modular templates (#2368)
Browse files Browse the repository at this point in the history
* Support non-modular templates

* Update docs

* Add tests

* Add fixtures

* Create eight-rules-drop.md
  • Loading branch information
cristiano-belloni authored May 3, 2023
1 parent 3cea60b commit 2ca7fcd
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-rules-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"modular-scripts": minor
---

Support non-modular templates
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Test File
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a README placeholder for this template fixture
14 changes: 14 additions & 0 deletions __fixtures__/templates/modular-template-non-modular/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "modular-template-non-modular",
"version": "1.1.0",
"exports": {
"./package.json": "./package.json"
},
"modular": {
"type": "template"
},
"license": "Apache-2.0",
"files": [
"src"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('I am a non-modular template');
4 changes: 3 additions & 1 deletion docs/how-to/create-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ To convert the package into a template, make the following changes to the
package's package.json:

- Change `modular.type` field to `"template"`
- Add `modular.templateType` with the desired target package type
- Optionally add `modular.templateType` with the desired target package type. If
`modular.templateType` is not present, the target package will not have a
`modular` field in its `packages.json`.

Example configuration for a template that generates `esm-view`s.

Expand Down
6 changes: 6 additions & 0 deletions docs/package-types/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ is passed to `modular add`:
}
```

## Templates for non-Modular packages

If `modular.templateType` is not set in the template package, the target package
will not have a `modular` field at all. This is useful to create non-Modular
templates which have their own configuration and scripts.

## Template contents and interpolation

All the files contained in the template package (optionally filtered by the
Expand Down
26 changes: 26 additions & 0 deletions packages/modular-scripts/src/__tests__/addPackage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const noFilterTemplatePath = path.join(
templatesPath,
'modular-template-no-filter',
);
const nonModularTemplatePath = path.join(
templatesPath,
'modular-template-non-modular',
);

// Temporary test context paths set by createTempModularRepoWithTemplate()
let tempModularRepo: string;
Expand Down Expand Up @@ -182,6 +186,28 @@ describe('When adding a module from a template without a files filter', () => {
});
});

describe('When adding a module from a template with a target non-modular workspace', () => {
let newModulePath: string;
beforeAll(async () => {
createTempModularRepoWithTemplate(nonModularTemplatePath);
setupMocks(tempModularRepo);
newModulePath = path.join(tempPackagesPath, 'non-modular-workspace');
await addPackageForTests('non-modular-workspace', 'non-modular');
});

it('generates the package.json', async () => {
// Expect name to be as set by command, and type as set by template
expect(await fs.readJSON(path.join(newModulePath, 'package.json')))
.toMatchInlineSnapshot(`
{
"name": "non-modular-workspace",
"private": false,
"version": "1.0.0",
}
`);
});
});

describe('When adding a module from a template with a files filter', () => {
let newModulePath: string;
beforeAll(async () => {
Expand Down
20 changes: 13 additions & 7 deletions packages/modular-scripts/src/addPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,18 @@ async function addPackage({
}

const modularTemplateType = modularTemplatePackageJson?.modular
?.templateType as string;
?.templateType as string | undefined;

if (
!['app', 'esm-view', 'view', 'source', 'package'].includes(
modularTemplateType,
!(
modularTemplateType === undefined ||
['app', 'esm-view', 'view', 'source', 'package'].includes(
modularTemplateType,
)
)
) {
throw new Error(
`${templateName} has modular type: ${modularTemplateType}, which does not exist, please use update this template`,
`${templateName} has modular type: ${modularTemplateType}, which does not exist, please update this template`,
);
}

Expand Down Expand Up @@ -243,9 +247,11 @@ async function addPackage({
{
name,
private: modularTemplateType === 'app',
modular: {
type: modularTemplateType,
},
modular: modularTemplateType
? {
type: modularTemplateType,
}
: undefined,
main: modularTemplatePackageJson?.main,
dependencies: modularTemplatePackageJson?.dependencies,
version: '1.0.0',
Expand Down

0 comments on commit 2ca7fcd

Please sign in to comment.