-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
87 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
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,87 @@ | ||
import { colors as c } from '@spotifly/utils'; | ||
import ini from 'ini'; | ||
import pkg from '../package.json'; | ||
import { readConfigWithPath } from './credentials'; | ||
import { Package } from './invoke'; | ||
|
||
const lb = '\n'; | ||
const lblb = '\n\n'; | ||
|
||
const versionHeader = (name: string, version: string) => { | ||
return c.bold(c.cyan(`${name} v${version}`)); | ||
}; | ||
const helpFooter = (homepage: string) => { | ||
return `For docs & help, visit ${homepage}`; | ||
}; | ||
const profiles = () => { | ||
try { | ||
const spotiflyConfigAndPath = readConfigWithPath(); | ||
if (!spotiflyConfigAndPath) throw new Error(); | ||
const [config, configPath] = spotiflyConfigAndPath; | ||
const parsedConfig = ini.parse(config); | ||
const profiles = Object.keys(parsedConfig) | ||
.map(p => '* ' + p) | ||
.join('\n'); | ||
return ( | ||
'Available profiles:' + | ||
lb + | ||
c.green(profiles) + | ||
lblb + | ||
`Config file: ${configPath}` | ||
); | ||
} catch (err) { | ||
return 'No profiles found, does your config file exist?'; | ||
} | ||
}; | ||
const fallback = (cmd: string) => { | ||
return ( | ||
c.yellow(`Unknown argument '${cmd}'`) + | ||
lblb + | ||
"Run 'spotifly --help' for available commands" | ||
); | ||
}; | ||
const subCommands = `Available subcommands: ${c.green(` | ||
* auth | ||
* library | ||
* profiles | ||
`)}`; | ||
const optionalFlags = `Optional global flags: | ||
${c.yellow('* --profile')} [string] | ||
The profile in your Spotifly config file to use for authentication. | ||
Defaults to 'default'. | ||
In order to see a list of available profiles, run ${c.green( | ||
'spotifly profiles' | ||
)}`; | ||
|
||
export default { | ||
packageHelp(pkg: Package, packageSpecificHelp: () => string) { | ||
return ( | ||
versionHeader(pkg.name, pkg.version) + | ||
lblb + | ||
packageSpecificHelp() + | ||
lblb + | ||
helpFooter(pkg.homepage) | ||
); | ||
}, | ||
mainHelp() { | ||
return ( | ||
versionHeader(pkg.name, pkg.version) + | ||
lb + | ||
`- ${pkg.description}` + | ||
lblb + | ||
subCommands + | ||
lb + | ||
optionalFlags + | ||
lblb + | ||
helpFooter(pkg.homepage) | ||
); | ||
}, | ||
profiles, | ||
version() { | ||
return versionHeader(pkg.name, pkg.version); | ||
}, | ||
fallback(cmd: string) { | ||
return fallback(cmd); | ||
}, | ||
}; |
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
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,46 @@ | ||
import commands from './commands'; | ||
import { | ||
credentialsFromConfig, | ||
getAccessToken, | ||
logError, | ||
profileFromArgv, | ||
readConfig, | ||
} from './credentials'; | ||
|
||
export type Package = Pick< | ||
typeof import('../package.json'), | ||
'name' | 'version' | 'homepage' | ||
>; | ||
|
||
export type InvokePackageArgs = { | ||
callback: (args: string[]) => unknown; | ||
help: () => string; | ||
pkg: Package; | ||
}; | ||
|
||
export const invokePackage = async ( | ||
argv: string[], | ||
tokenFlag: string | null, | ||
{ callback, help, pkg }: InvokePackageArgs | ||
): Promise<unknown> => { | ||
// Invoke package-specific help | ||
if (argv.includes('--help') || argv.includes('-h')) { | ||
console.info(commands.packageHelp(pkg, help)); | ||
return; | ||
} | ||
// If the package doesn't need a token, just invoke it | ||
if (!tokenFlag || argv.includes(tokenFlag)) return callback(argv); | ||
|
||
const spotiflyConfig = readConfig(); | ||
if (!spotiflyConfig) return callback(argv); | ||
|
||
const profile = profileFromArgv(argv); | ||
try { | ||
const credentials = credentialsFromConfig(spotiflyConfig, profile); | ||
const { access_token } = await getAccessToken(credentials); | ||
|
||
return callback([...argv, tokenFlag, access_token]); | ||
} catch (err) { | ||
logError(err); | ||
} | ||
}; |
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