Skip to content

Commit

Permalink
feat(lm): add lm and lm:info command
Browse files Browse the repository at this point in the history
  • Loading branch information
keiko713 committed Oct 29, 2020
1 parent a3b7606 commit fa207e8
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/commands/lm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { Command } = require('@oclif/command')
const showHelp = require('../../utils/show-help')
const { isEmptyCommand } = require('../../utils/check-command-inputs')

class LmCommand extends Command {
async run() {
const { flags, args } = this.parse(LmCommand)

// Show help on empty sub command
if (isEmptyCommand(flags, args)) {
showHelp(this.id)
this.exit()
}
}
}

LmCommand.description = `Handle Large Media operations
The lm command will help you manage a large media for the site
`
LmCommand.examples = [
'netlify lm:info',
// 'netlify lm:install',
// 'netlify lm:setup'
]

LmCommand.hidden = true

module.exports = LmCommand
42 changes: 42 additions & 0 deletions src/commands/lm/info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { Command } = require('@oclif/command')
const Listr = require('listr')
const chalk = require('chalk')
const {
GitValidators,
checkLFSFilters,
checkHelperVersion
} = require('../../utils/lm/requirements')

class LmInfoCommand extends Command {
async run() {
const steps = GitValidators
steps.push(
{
title: 'Checking Git LFS filters',
task: async () => {
const installed = await checkLFSFilters()
if (!installed) {
throw new Error('Git LFS filters are not installed, run `git lfs install` to install them')
}
}
},
{
title: `Checking Netlify's Git Credentials version`,
task: async (ctx, task) => {
const version = await checkHelperVersion()
task.title += chalk.dim(` [${version}]`)
}
}
)

const tasks = new Listr(steps, { concurrent: true, exitOnError: false })
tasks.run().catch((err) => { })
}
}

LmInfoCommand.description = `Show large media requirements info.`
LmInfoCommand.hidden = true

LmInfoCommand.examples = ['netlify lm:info']

module.exports = LmInfoCommand
64 changes: 64 additions & 0 deletions src/utils/lm/requirements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const chalk = require('chalk')
const execa = require('execa')
const semver = require('semver')

module.exports.GitValidators = [
{
title: 'Checking Git version',
task: async (ctx, task) => {
const version = await checkGitVersion()
task.title += chalk.dim(` [${version}]`)
}
},
{
title: 'Checking Git LFS version',
task: async (ctx, task) => {
const version = await checkLFSVersion()
task.title += chalk.dim(` [${version}]`)
}
}
]

module.exports.checkLFSFilters = async function checkLFSFilters() {
try {
const result = await execa('git', ['config', '--get-regexp', 'filter.lfs'])
return Promise.resolve(result.stdout.length > 0)
} catch (error) {
return Promise.resolve(false)
}
}

module.exports.checkHelperVersion = async function checkHelperVersion() {
try {
const result = await execa('git-credential-netlify', ['--version'])
return matchVersion(result.stdout, /git-credential-netlify\/([\.\d]+).*/, '0.1.1', `Invalid Netlify's Git Credential version. Please update to version 2.5.1 or above`)
} catch (error) {
throw new Error(`Check that Netlify's Git Credential helper is installed and updated to the latest version`)
}
}

async function checkGitVersion() {
try {
const result = await execa('git', ['--version'])
return Promise.resolve(result.stdout.split(' ').pop())
} catch (error) {
throw new Error('Check that Git is installed in your system')
}
}

async function checkLFSVersion() {
try {
const result = await execa('git-lfs', ['--version'])
return matchVersion(result.stdout, /git-lfs\/([\.\d]+).*/, '2.5.1', 'Invalid Git LFS version. Please update to version 2.5.1 or above')
} catch (error) {
throw new Error('Check that Git LFS is installed in your system')
}
}

function matchVersion(out, regex, version, message) {
const match = out.match(regex)
if (!match || match.length != 2 || semver.lt(match[1], version)) {
throw new Error(message)
}
return Promise.resolve(match[1])
}

0 comments on commit fa207e8

Please sign in to comment.