Skip to content

Instantly share code, notes, and snippets.

@moltar
Created May 4, 2020 15:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moltar/afe0e541ac87ed07f1454520683140de to your computer and use it in GitHub Desktop.
Save moltar/afe0e541ac87ed07f1454520683140de to your computer and use it in GitHub Desktop.
#!/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')
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/commit@v2.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
commit-message: 'chore: replaces template variables'
rebase: 'true'
@moltar
Copy link
Author

moltar commented May 4, 2020

Make sure to replace TEMPLATE_GITHUB_REPOSITORY with your template repo name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment