Created
May 4, 2020 15:58
-
-
Save moltar/afe0e541ac87ed07f1454520683140de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const { writeFileSync, readFileSync, unlinkSync } = require('fs') | |
const package = require('./package.json') | |
const packageLock = require('./package-lock.json') | |
const TEMPLATE_GITHUB_REPOSITORY = 'org/template' | |
const { GITHUB_REPOSITORY } = process.env | |
if (GITHUB_REPOSITORY === TEMPLATE_GITHUB_REPOSITORY) { | |
// eslint-disable-next-line no-console | |
console.info(`Not running inside ${TEMPLATE_GITHUB_REPOSITORY} repo.`) | |
process.exit() | |
} | |
if (!GITHUB_REPOSITORY) { | |
throw new Error('Unknown GITHUB_REPOSITORY.') | |
} | |
const TEMPLATE_PACKAGE_NAME = package.name | |
const PACKAGE_NAME = `@${GITHUB_REPOSITORY.toLowerCase()}` | |
/** | |
* package.json | |
*/ | |
package.name = PACKAGE_NAME | |
package.homepage = package.homepage.replace(TEMPLATE_GITHUB_REPOSITORY, GITHUB_REPOSITORY) | |
writeFileSync('./package.json', JSON.stringify(package, undefined, 2), { encoding: 'utf8' }) | |
/** | |
* package-lock.json | |
*/ | |
packageLock.name = PACKAGE_NAME | |
writeFileSync('./package-lock.json', JSON.stringify(packageLock, undefined, 2), { | |
encoding: 'utf8', | |
}) | |
/** | |
* README | |
*/ | |
const readme = readFileSync('./README.md', { encoding: 'utf8' }) | |
const newReadme = readme | |
.split(TEMPLATE_PACKAGE_NAME) | |
.join(PACKAGE_NAME) | |
.split(TEMPLATE_GITHUB_REPOSITORY) | |
.join(GITHUB_REPOSITORY) | |
writeFileSync('./README.md', newReadme, { encoding: 'utf8' }) | |
/** | |
* CLEAN UP | |
*/ | |
unlinkSync('./on-template.js') | |
unlinkSync('./.github/workflows/on-template.yml') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: On Template | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
npm-publish: | |
name: on template | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v1 | |
- name: Setup Node | |
uses: actions/setup-node@v1 | |
with: | |
node-version: 12 | |
- name: Replace | |
run: node ./on-template.js | |
- name: push | |
uses: github-actions-x/[email protected] | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
commit-message: 'chore: replaces template variables' | |
rebase: 'true' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure to replace
TEMPLATE_GITHUB_REPOSITORY
with your template repo name.