-
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.
* add profiles command * add changeset * add docs and changeset * fix docs about convenience methods * fix typo
- Loading branch information
Showing
15 changed files
with
1,452 additions
and
113 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,5 @@ | ||
--- | ||
'@spotifly/cli': minor | ||
--- | ||
|
||
The core CLI now supports the `profiles` command to list available Spotifly profiles. |
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
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,89 @@ | ||
import { colors as c } from '@spotifly/utils'; | ||
import ini from 'ini'; | ||
import pkg from '../package.json'; | ||
import { readConfigWithPath } from './credentials'; | ||
|
||
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: | ||
${c.green(profiles)} | ||
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 | ||
`)} | ||
Optional global flags: | ||
${c.yellow('* --profile')} [string] | ||
The profile in your Spotifly config file to use for authentication. | ||
Defaults to 'default'. | ||
- In order to get help for a specific subcommand, run ${c.green( | ||
'spotifly <subcommand> --help', | ||
)} | ||
- In order to see a list of available profiles, run ${c.green( | ||
'spotifly profiles', | ||
)}`; | ||
|
||
export default { | ||
packageHelp( | ||
packageName: string, | ||
packageVersion: string, | ||
packageSpecificHelp: () => string, | ||
) { | ||
return ( | ||
versionHeader(packageName, packageVersion) + | ||
lblb + | ||
packageSpecificHelp() + | ||
lblb + | ||
helpFooter(pkg.homepage) | ||
); | ||
}, | ||
mainHelp() { | ||
return ( | ||
versionHeader(pkg.name, pkg.version) + | ||
lb + | ||
`- ${pkg.description}` + | ||
lblb + | ||
subCommands + | ||
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,43 @@ | ||
import commands from './commands'; | ||
import { | ||
credentialsFromConfig, | ||
getAccessToken, | ||
logError, | ||
profileFromArgv, | ||
readConfig, | ||
} from './credentials'; | ||
|
||
export type InvokePackageArgs = { | ||
callback: (args: string[]) => unknown; | ||
help: () => string; | ||
packageName: string; | ||
packageVersion: string; | ||
}; | ||
|
||
export const invokePackage = async ( | ||
argv: string[], | ||
tokenFlag: string | null, | ||
{ callback, help, packageName, packageVersion }: InvokePackageArgs, | ||
): Promise<unknown> => { | ||
// Invoke package-specific help | ||
if (argv.includes('--help') || argv.includes('-h')) { | ||
console.info(commands.packageHelp(packageName, packageVersion, 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); | ||
console.info(`Using profile "${profile}"`); | ||
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
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
Oops, something went wrong.