Skip to content

Commit

Permalink
Merge pull request #9 from GrigoryanSasun/log-final-generated-url
Browse files Browse the repository at this point in the history
feat: logFinalVersion cli flag added which logs the final version int…
  • Loading branch information
citizensas authored Feb 22, 2020
2 parents c5ed8df + 2cd286d commit 8859483
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/cliFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface IFlags {
remoteName: string
protocol: string
token: string
logFinalVersion?: boolean
}

let flags: IFlags
Expand Down
31 changes: 29 additions & 2 deletions src/generate-preview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {MODULE_DIR, TMP_DIR} from './constants'

import * as path from 'path'
import * as fs from 'fs'

import {getBranchName} from './getBranchName'
import {getRemoteUrl} from './getRemoteUrl'
Expand All @@ -12,6 +13,27 @@ import {cleanup} from './cleanup'
import {IFlags, setFlags} from './cliFlags'
import {logger} from './logger'

const logFileName = 'generate-preview.log'

const writeIntoLogFile = (content: string) => {
const path = logFileName
return new Promise((resolve, reject) => {
fs.open(path, 'w+', function(err) {
if (err) {
reject(err)
} else {
fs.writeFile(path, content, function(err) {
if (err) {
reject(err)
} else {
resolve()
}
})
}
})
})
}

export function generatePreview(flags: IFlags) {
setFlags(flags)
Promise.all([getBranchName(), getRemoteUrl(), npmPack()])
Expand All @@ -38,9 +60,14 @@ export function generatePreview(flags: IFlags) {
logger.info(
`Now you can install generated URL in you module using Yarn or NPM. Just run the following command.`
)
console.log(`npm install ${res.remoteUrl}#${res.distBranchName}`)
const version = `${res.remoteUrl}#${res.distBranchName}`
console.log(`npm install ${version}`)
console.log(' OR ')
console.log(`yarn add ${res.remoteUrl}#${res.distBranchName}`)
console.log(`yarn add ${version}`)
if (flags.logFinalVersion) {
console.log(`Logging the final version into ${logFileName}...`)
return writeIntoLogFile(version)
}
})
)
.catch(err => {
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ program
.option('-p, --protocol [protocol]', 'git protocol (i.e. git+ssh, https)', REMOTE_URL_TYPES.GIT_SSH)
.option('--verbose', 'prints higher level of logs')
.option('--token [token]', 'git user token for remote authentication', '')
.option('--logFinalVersion', 'whether should log the final version into the generate-preview.log file')

program.parse(process.argv)

Expand All @@ -22,5 +23,6 @@ logger.info(`Running generate-preview with version ${pkg.version}`)
generatePreview({
remoteName: program.remote,
protocol: program.protocol,
token: program.token
token: program.token,
logFinalVersion: program.logFinalVersion
})

0 comments on commit 8859483

Please sign in to comment.