Skip to content

Commit

Permalink
refactor: move license and readme templates to preset-common
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Dec 12, 2023
1 parent 994a701 commit da223b2
Show file tree
Hide file tree
Showing 18 changed files with 492 additions and 16 deletions.
34 changes: 32 additions & 2 deletions packages/preset-common/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
# preset-common
# @solana-developers/preset-common

This library is a preset for [create-solana-dapp](https://npm.im/create-solana-dapp) with shared code for the other
presets.
-presets.

## Supported generators

### common-template

```bash
> NX generate @solana-developers/preset-common:common-template [name] [options,...]

From: @solana-developers/preset-common (v0.0.1)
Name: common-template

Generate Common templates

Options:
--directory [string]
--name [string]
--npmScope The npm scope to use [string]
--template The template to use [string] [choices:
"license", "readme"]
--webName Name of the web application [string]
(overrides name)
--anchor Anchor template to use [string] [choices: "none",
"counter", "hello-world"]
[default: "none"]
--anchorName Anchor project name [string] [default: "anchor"]
--licenseAuthor The author to use in the [string]
license
--preset The preset to use [string] [choices: "next",
"react"]
```

## More information

Expand Down
9 changes: 9 additions & 0 deletions packages/preset-common/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"generators": {
"common-template": {
"factory": "./src/generators/common-template/common-template-generator",
"schema": "./src/generators/common-template/common-template-schema.json",
"description": "common-template generator"
}
}
}
3 changes: 2 additions & 1 deletion packages/preset-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
"name": "Bram Borggreve",
"url": "https://github.com/beeman"
}
]
],
"generators": "./generators.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`common-template generator should generate files for { template: 'license', anchor: 'counter' } template with %s anchor 1`] = `
{
"LICENSE": {
"content": [
"MIT License",
"Copyright (c) 2023 test",
"Permission is hereby granted, free of charge, to any person obtaining a copy",
"of this software and associated documentation files (the "Software"), to deal",
"in the Software without restriction, including without limitation the rights",
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell",
"copies of the Software, and to permit persons to whom the Software is",
"furnished to do so, subject to the following conditions:",
"The above copyright notice and this permission notice shall be included in all",
"copies or substantial portions of the Software.",
"THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR",
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE",
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE",
"SOFTWARE.",
],
"isBinary": false,
"path": "test/LICENSE",
},
}
`;

exports[`common-template generator should generate files for { template: 'license', anchor: 'hello-world' } template with %s anchor 1`] = `
{
"LICENSE": {
"content": [
"MIT License",
"Copyright (c) 2023 test",
"Permission is hereby granted, free of charge, to any person obtaining a copy",
"of this software and associated documentation files (the "Software"), to deal",
"in the Software without restriction, including without limitation the rights",
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell",
"copies of the Software, and to permit persons to whom the Software is",
"furnished to do so, subject to the following conditions:",
"The above copyright notice and this permission notice shall be included in all",
"copies or substantial portions of the Software.",
"THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR",
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE",
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE",
"SOFTWARE.",
],
"isBinary": false,
"path": "test/LICENSE",
},
}
`;

exports[`common-template generator should generate files for { template: 'license', anchor: 'none' } template with %s anchor 1`] = `
{
"LICENSE": {
"content": [
"MIT License",
"Copyright (c) 2023 test",
"Permission is hereby granted, free of charge, to any person obtaining a copy",
"of this software and associated documentation files (the "Software"), to deal",
"in the Software without restriction, including without limitation the rights",
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell",
"copies of the Software, and to permit persons to whom the Software is",
"furnished to do so, subject to the following conditions:",
"The above copyright notice and this permission notice shall be included in all",
"copies or substantial portions of the Software.",
"THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR",
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,",
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE",
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER",
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,",
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE",
"SOFTWARE.",
],
"isBinary": false,
"path": "test/LICENSE",
},
}
`;

exports[`common-template generator should generate files for { template: 'readme', anchor: 'counter' } template with %s anchor 1`] = `
{
"README.md": {
"content": [
"# test",
"This project is generated with the [create-solana-dapp](https://github.com/solana-developers/create-solana-dapp) generator.",
"## Getting Started",
"### Prerequisites",
"- Node v18.18.0 or higher",
"- Rust v1.70.0 or higher",
"- Anchor CLI 0.29.0 or higher",
"- Solana CLI 1.17.0 or higher",
"### Installation",
"#### Clone the repo",
"\`\`\`shell",
"git clone <repo-url>",
"cd <repo-name>",
"\`\`\`",
"#### Install Dependencies",
"\`\`\`shell",
"yarn install",
"\`\`\`",
"#### Start the web app",
"\`\`\`",
"yarn dev",
"\`\`\`",
"## Apps",
"### anchor",
"This is a Solana program written in Rust using the Anchor framework.",
"#### Commands",
"You can use any normal anchor commands. Either move to the \`anchor\` directory and run the \`anchor\` command or prefix the command with \`yarn\`, eg: \`yarn anchor\`.",
"#### Sync the program id:",
"Running this command will create a new keypair in the \`anchor/target/deploy\` directory and save the address to the Anchor config file and update the \`declare_id!\` macro in the \`./src/lib.rs\` file of the program.",
"You will manually need to update the constant in \`anchor/lib/counter-exports.ts\` to match the new program id.",
"\`\`\`shell",
"yarn anchor keys sync",
"\`\`\`",
"#### Build the program:",
"\`\`\`shell",
"yarn anchor-build",
"\`\`\`",
"#### Start the test validator with the program deployed:",
"\`\`\`shell",
"yarn anchor-localnet",
"\`\`\`",
"#### Run the tests",
"\`\`\`shell",
"yarn anchor-test",
"\`\`\`",
"#### Deploy to Devnet",
"\`\`\`shell",
"yarn anchor deploy --provider.cluster devnet",
"\`\`\`",
"### web",
"This is a React app that uses the Anchor generated client to interact with the Solana program.",
"#### Commands",
"Start the web app",
"\`\`\`shell",
"yarn dev",
"\`\`\`",
"Build the web app",
"\`\`\`shell",
"yarn build",
"\`\`\`",
],
"isBinary": false,
"path": "test/README.md",
},
}
`;

exports[`common-template generator should generate files for { template: 'readme', anchor: 'hello-world' } template with %s anchor 1`] = `
{
"README.md": {
"content": [
"# test",
"This project is generated with the [create-solana-dapp](https://github.com/solana-developers/create-solana-dapp) generator.",
"## Getting Started",
"### Prerequisites",
"- Node v18.18.0 or higher",
"- Rust v1.70.0 or higher",
"- Anchor CLI 0.29.0 or higher",
"- Solana CLI 1.17.0 or higher",
"### Installation",
"#### Clone the repo",
"\`\`\`shell",
"git clone <repo-url>",
"cd <repo-name>",
"\`\`\`",
"#### Install Dependencies",
"\`\`\`shell",
"yarn install",
"\`\`\`",
"#### Start the web app",
"\`\`\`",
"yarn dev",
"\`\`\`",
"## Apps",
"### anchor",
"This is a Solana program written in Rust using the Anchor framework.",
"#### Commands",
"You can use any normal anchor commands. Either move to the \`anchor\` directory and run the \`anchor\` command or prefix the command with \`yarn\`, eg: \`yarn anchor\`.",
"#### Sync the program id:",
"Running this command will create a new keypair in the \`anchor/target/deploy\` directory and save the address to the Anchor config file and update the \`declare_id!\` macro in the \`./src/lib.rs\` file of the program.",
"You will manually need to update the constant in \`anchor/lib/hello-world-exports.ts\` to match the new program id.",
"\`\`\`shell",
"yarn anchor keys sync",
"\`\`\`",
"#### Build the program:",
"\`\`\`shell",
"yarn anchor-build",
"\`\`\`",
"#### Start the test validator with the program deployed:",
"\`\`\`shell",
"yarn anchor-localnet",
"\`\`\`",
"#### Run the tests",
"\`\`\`shell",
"yarn anchor-test",
"\`\`\`",
"#### Deploy to Devnet",
"\`\`\`shell",
"yarn anchor deploy --provider.cluster devnet",
"\`\`\`",
"### web",
"This is a React app that uses the Anchor generated client to interact with the Solana program.",
"#### Commands",
"Start the web app",
"\`\`\`shell",
"yarn dev",
"\`\`\`",
"Build the web app",
"\`\`\`shell",
"yarn build",
"\`\`\`",
],
"isBinary": false,
"path": "test/README.md",
},
}
`;

exports[`common-template generator should generate files for { template: 'readme', anchor: 'none' } template with %s anchor 1`] = `
{
"README.md": {
"content": [
"# test",
"This project is generated with the [create-solana-dapp](https://github.com/solana-developers/create-solana-dapp) generator.",
"## Getting Started",
"### Prerequisites",
"- Node v18.18.0 or higher",
"### Installation",
"#### Clone the repo",
"\`\`\`shell",
"git clone <repo-url>",
"cd <repo-name>",
"\`\`\`",
"#### Install Dependencies",
"\`\`\`shell",
"yarn install",
"\`\`\`",
"#### Start the web app",
"\`\`\`",
"yarn dev",
"\`\`\`",
"## Apps",
"### web",
"This is a React app.",
"#### Commands",
"Start the web app",
"\`\`\`shell",
"yarn dev",
"\`\`\`",
"Build the web app",
"\`\`\`shell",
"yarn build",
"\`\`\`",
],
"isBinary": false,
"path": "test/README.md",
},
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Tree } from '@nx/devkit'
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'
import { getRecursiveFileContents } from '../../utils/get-recursive-file-contents'
import { commonTemplateGenerator } from './common-template-generator'
import { CommonTemplateSchema } from './common-template-schema'

describe('common-template generator', () => {
let tree: Tree
const options: CommonTemplateSchema = {
name: 'test',
template: 'license',
directory: 'test',
anchor: 'none',
anchorName: 'anchor',
npmScope: 'test',
webName: 'test',
}
const templates: CommonTemplateSchema['template'][] = ['license', 'readme']
const anchor: CommonTemplateSchema['anchor'][] = ['none', 'counter', 'hello-world']

const matrix = templates.map((template) => anchor.map((anchor) => ({ template, anchor }))).flat()
process.env['USER'] = 'test'

beforeEach(() => {
tree = createTreeWithEmptyWorkspace()
})

it.each(matrix)('should generate files for %s template with %s anchor', async ({ template, anchor }) => {
await commonTemplateGenerator(tree, { ...options, template, anchor })

const contents = getRecursiveFileContents(tree, options.directory)
expect(contents).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { generateFiles, Tree } from '@nx/devkit'
import * as path from 'path'
import { genericSubstitutions } from '../../utils/generic-substitutions'
import { CommonTemplateSchema } from './common-template-schema'

export async function commonTemplateGenerator(tree: Tree, options: CommonTemplateSchema, templatePath: string = '') {
const substitutions = genericSubstitutions({
anchor: options.anchor,
anchorName: options.anchorName,
licenseAuthor: options.licenseAuthor,
name: options.webName,
npmScope: options.npmScope,
preset: options.preset,
})
generateFiles(tree, path.join(__dirname, 'files', options.template, templatePath), options.directory, {
...options,
...substitutions,
fileNameUnderscore: substitutions.fileName.replace(/-/g, '_'),
})
}

export default commonTemplateGenerator
Loading

0 comments on commit da223b2

Please sign in to comment.