-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lm): add lm and lm:info command
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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]) | ||
} |