Skip to content

Commit

Permalink
git-install
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Jul 24, 2024
1 parent edc6620 commit e72827a
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*

git-deps
git-install-lock

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ WORKDIR /var/app
COPY . /var/app
RUN yarn install
RUN yarn build
RUN node git-install.js -c

FROM node:16.1-alpine

Expand Down
Empty file added git-deps/.gitkeep
Empty file.
125 changes: 125 additions & 0 deletions git-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const fs = require('fs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)

const package = require('./package.json')

const { dependencies, gitDependencies } = package

const dir = 'git-deps'

let clc = {}
for (const [key, code] of Object.entries({
cyanBright: '\x1b[36m',
yellowBright: '\x1b[33m',
greenBright: '\x1b[32m',
})) {
clc[key] = (...args) => {
return code + [...args].join(' ') + '\x1b[0m'
}
}

const logDep = (color, ...args) => {
let msg = [' -', ...args]
if (color) {
msg = [clc[color](...msg)]
}
console.log(...msg)
}

async function main() {
const { argv } = process
if (argv[2] === '-c' || argv[2] === '--cleanup') {
const files = await fs.promises.readdir(dir)
for (const f of files) {
if (f !== '.gitignore') {
fs.rmSync(dir + '/' + f, { recursive: true, force: true })
}
}
return
}

if (!gitDependencies) {
console.log('No gitDependencies in package.json, so nothing to preinstall.')
return
}

const lockFile = 'git-install-lock'
if (fs.existsSync(lockFile)) {
console.error(lockFile, 'exists, so cannot run. It is for recursion protection.')
return
}
fs.writeFileSync(lockFile, '1')

console.log(clc.cyanBright('preinstalling deps...'))

if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
process.chdir(dir)

const gitDeps = Object.entries(gitDependencies)
const deps = {}
for (const [ key, dep ] of gitDeps) {
const url = new URL(dep)

const { pathname } = url
const [ repo, branchFolder ] = pathname.split('/tree/')
let branch = '', folder = ''
if (branchFolder) {
const parts = branchFolder.split('/')
branch = ' -b ' + parts[0]
if (parts[1]) {
parts.shift()
folder = parts.join('/')
}
}

const commit = url.hash.replace('#', '')
const repoName = repo.split('/')[2]

deps[key] = { dep, repo, branch, folder, repoName, commit }

if (fs.existsSync(repoName)) {
logDep('yellowBright', repoName, 'already cloned, using as cache.')
continue
}

deps[key].cloned = true

const clone = 'git clone ' + url.origin + repo + branch
logDep('greenBright', clone)
await exec(clone)

if (commit) {
process.chdir(repoName)
const resetTo = 'git reset --hard ' + commit
logDep(null, '-', resetTo)
await exec(resetTo)
process.chdir('..')
}
}

console.log(' ')
console.log(clc.cyanBright('yarn-adding cloned deps (if not added)...'))

for (const [ key, dep ] of Object.entries(deps)) {
let path = './' + dir + '/' + dep.repoName
if (dep.folder) {
path += '/' + dep.folder
}

if (dep.cloned || dependencies[key] !== path) {
const add = 'yarn add ' + path
logDep('greenBright', add)
await exec(add)
}
}

console.log(clc.greenBright('ok'))
console.log('')

fs.unlinkSync('../' + lockFile)
}

main()
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"connected-react-router": "^6.9.2",
"counterpart": "^0.18.6",
"emoji-picker-element": "^1.10.1",
"formik": "https://gitpkg.now.sh/golos-blockchain/formik/packages/formik?3b21166c33ade760d562091e1fa0b71d172a7aaf",
"formik": "./git-deps/formik/packages/formik",
"git-rev-sync": "^3.0.2",
"golos-lib-js": "^0.9.69",
"history": "4.10.1",
Expand All @@ -33,7 +33,7 @@
"react-dom": "^17.0.2",
"react-dom-confetti": "^0.2.0",
"react-dropzone": "^14.2.3",
"react-foundation-components": "git+https://github.com/golos-blockchain/react-foundation-components.git#45bcb22dd3e078a515a9f7dfb33f834b31f83a43",
"react-foundation-components": "./git-deps/react-foundation-components",
"react-intl": "^5.24.6",
"react-notification": "^6.8.5",
"react-redux": "^7.2.6",
Expand Down Expand Up @@ -66,7 +66,12 @@
"react-app-rewired": "^2.1.11",
"react-scripts": "^5.0.0"
},
"gitDependencies": {
"formik": "https://github.com/golos-blockchain/formik/tree/master/packages/formik#3b21166c33ade760d562091e1fa0b71d172a7aaf",
"react-foundation-components": "https://github.com/golos-blockchain/react-foundation-components#45bcb22dd3e078a515a9f7dfb33f834b31f83a43"
},
"scripts": {
"preinstall": "node git-install.js",
"cordova": "cordova",
"dev": "react-app-rewired start",
"dev:server": "nodemon server",
Expand Down
6 changes: 2 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5031,9 +5031,8 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

"formik@https://gitpkg.now.sh/golos-blockchain/formik/packages/formik?3b21166c33ade760d562091e1fa0b71d172a7aaf":
formik@./git-deps/formik/packages/formik:
version "2.2.9"
resolved "https://gitpkg.now.sh/golos-blockchain/formik/packages/formik?3b21166c33ade760d562091e1fa0b71d172a7aaf#0f72ba16b0610fc14a6d42dfe2556600df5a3132"
dependencies:
deepmerge "^2.1.1"
hoist-non-react-statics "^3.3.0"
Expand Down Expand Up @@ -8449,9 +8448,8 @@ react-fast-compare@^2.0.1:
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-2.0.4.tgz#e84b4d455b0fec113e0402c329352715196f81f9"
integrity sha512-suNP+J1VU1MWFKcyt7RtjiSWUjvidmQSlqu+eHslq+342xCbGTYmC0mEhPCOHxlW0CywylOC1u2DFAT+bv4dBw==

"react-foundation-components@git+https://github.com/golos-blockchain/react-foundation-components.git#45bcb22dd3e078a515a9f7dfb33f834b31f83a43":
react-foundation-components@./git-deps/react-foundation-components:
version "0.14.0"
resolved "git+https://github.com/golos-blockchain/react-foundation-components.git#45bcb22dd3e078a515a9f7dfb33f834b31f83a43"
dependencies:
babel-runtime "^6.25.0"
classnames "^2.2.5"
Expand Down

0 comments on commit e72827a

Please sign in to comment.