@socketsecurity/cli 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +5 -0
  2. package/cli.js +12 -1
  3. package/lib/commands/audit-log/index.js +162 -0
  4. package/lib/commands/cdxgen/index.js +211 -0
  5. package/lib/commands/dependencies/index.js +150 -0
  6. package/lib/commands/index.js +11 -3
  7. package/lib/commands/info/index.js +123 -81
  8. package/lib/commands/login/index.js +1 -1
  9. package/lib/commands/logout/index.js +1 -1
  10. package/lib/commands/npm/index.js +8 -3
  11. package/lib/commands/npx/index.js +1 -1
  12. package/lib/commands/raw-npm/index.js +59 -0
  13. package/lib/commands/raw-npx/index.js +59 -0
  14. package/lib/commands/report/create.js +1 -1
  15. package/lib/commands/report/index.js +1 -1
  16. package/lib/commands/report/view.js +1 -1
  17. package/lib/commands/repos/create.js +166 -0
  18. package/lib/commands/repos/delete.js +93 -0
  19. package/lib/commands/repos/index.js +30 -0
  20. package/lib/commands/repos/list.js +170 -0
  21. package/lib/commands/repos/update.js +166 -0
  22. package/lib/commands/repos/view.js +128 -0
  23. package/lib/commands/scan/create.js +245 -0
  24. package/lib/commands/scan/delete.js +112 -0
  25. package/lib/commands/scan/index.js +30 -0
  26. package/lib/commands/scan/list.js +192 -0
  27. package/lib/commands/scan/metadata.js +113 -0
  28. package/lib/commands/scan/stream.js +115 -0
  29. package/lib/commands/wrapper/index.js +199 -0
  30. package/lib/flags/command.js +14 -0
  31. package/lib/flags/index.js +1 -0
  32. package/lib/shadow/npm-injection.cjs +11 -1
  33. package/lib/utils/format-issues.js +28 -1
  34. package/lib/utils/meow-with-subcommands.js +1 -2
  35. package/lib/utils/misc.js +0 -1
  36. package/lib/utils/path-resolve.js +31 -6
  37. package/lib/utils/sdk.js +0 -3
  38. package/package.json +79 -62
@@ -0,0 +1,166 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import chalk from 'chalk'
4
+ import meow from 'meow'
5
+ import ora from 'ora'
6
+
7
+ import { outputFlags } from '../../flags/index.js'
8
+ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
9
+ import { prepareFlags } from '../../utils/flags.js'
10
+ import { printFlagList } from '../../utils/formatting.js'
11
+ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
12
+
13
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
14
+ export const create = {
15
+ description: 'Create a repository in an organization',
16
+ async run (argv, importMeta, { parentName }) {
17
+ const name = parentName + ' create'
18
+
19
+ const input = setupCommand(name, create.description, argv, importMeta)
20
+ if (input) {
21
+ const spinnerText = 'Creating repository... \n'
22
+ const spinner = ora(spinnerText).start()
23
+ await createRepo(input.orgSlug, input, spinner)
24
+ }
25
+ }
26
+ }
27
+
28
+ const repositoryCreationFlags = prepareFlags({
29
+ repoName: {
30
+ type: 'string',
31
+ shortFlag: 'n',
32
+ default: '',
33
+ description: 'Repository name',
34
+ },
35
+ repoDescription: {
36
+ type: 'string',
37
+ shortFlag: 'd',
38
+ default: '',
39
+ description: 'Repository description',
40
+ },
41
+ homepage: {
42
+ type: 'string',
43
+ shortFlag: 'h',
44
+ default: '',
45
+ description: 'Repository url',
46
+ },
47
+ defaultBranch: {
48
+ type: 'string',
49
+ shortFlag: 'b',
50
+ default: 'main',
51
+ description: 'Repository default branch',
52
+ },
53
+ visibility: {
54
+ type: 'string',
55
+ shortFlag: 'v',
56
+ default: 'private',
57
+ description: 'Repository visibility (Default Private)',
58
+ }
59
+ })
60
+
61
+ // Internal functions
62
+
63
+ /**
64
+ * @typedef CommandContext
65
+ * @property {boolean} outputJson
66
+ * @property {boolean} outputMarkdown
67
+ * @property {string} orgSlug
68
+ * @property {string} name
69
+ * @property {string} description
70
+ * @property {string} homepage
71
+ * @property {string} default_branch
72
+ * @property {string} visibility
73
+ */
74
+
75
+ /**
76
+ * @param {string} name
77
+ * @param {string} description
78
+ * @param {readonly string[]} argv
79
+ * @param {ImportMeta} importMeta
80
+ * @returns {void|CommandContext}
81
+ */
82
+ function setupCommand (name, description, argv, importMeta) {
83
+ const flags = {
84
+ ...outputFlags,
85
+ ...repositoryCreationFlags
86
+ }
87
+
88
+ const cli = meow(`
89
+ Usage
90
+ $ ${name} <org slug>
91
+
92
+ Options
93
+ ${printFlagList(flags, 6)}
94
+
95
+ Examples
96
+ $ ${name} FakeOrg --repoName=test-repo
97
+ `, {
98
+ argv,
99
+ description,
100
+ importMeta,
101
+ flags
102
+ })
103
+
104
+ const {
105
+ json: outputJson,
106
+ markdown: outputMarkdown,
107
+ repoName,
108
+ repoDescription,
109
+ homepage,
110
+ defaultBranch,
111
+ visibility
112
+ } = cli.flags
113
+
114
+ const [orgSlug = ''] = cli.input
115
+
116
+ if (!orgSlug) {
117
+ console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`)
118
+ cli.showHelp()
119
+ return
120
+ }
121
+
122
+ if (!repoName) {
123
+ console.error(`${chalk.bgRed('Input error')}: Repository name is required. \n`)
124
+ cli.showHelp()
125
+ return
126
+ }
127
+
128
+ return {
129
+ outputJson,
130
+ outputMarkdown,
131
+ orgSlug,
132
+ name: repoName,
133
+ description: repoDescription,
134
+ homepage,
135
+ default_branch: defaultBranch,
136
+ visibility
137
+ }
138
+ }
139
+
140
+ /**
141
+ * @typedef RepositoryData
142
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'createOrgRepo'>["data"]} data
143
+ */
144
+
145
+ /**
146
+ * @param {string} orgSlug
147
+ * @param {CommandContext} input
148
+ * @param {import('ora').Ora} spinner
149
+ * @returns {Promise<void|RepositoryData>}
150
+ */
151
+ async function createRepo (orgSlug, input, spinner) {
152
+ const socketSdk = await setupSdk(getDefaultKey())
153
+ const result = await handleApiCall(socketSdk.createOrgRepo(orgSlug, input), 'creating repository')
154
+
155
+ if (!result.success) {
156
+ return handleUnsuccessfulApiResponse('createOrgRepo', result, spinner)
157
+ }
158
+
159
+ spinner.stop()
160
+
161
+ console.log('\n✅ Repository created successfully \n')
162
+
163
+ return {
164
+ data: result.data
165
+ }
166
+ }
@@ -0,0 +1,93 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import chalk from 'chalk'
4
+ import meow from 'meow'
5
+ import ora from 'ora'
6
+
7
+ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
8
+ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
9
+
10
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
11
+ export const del = {
12
+ description: 'Delete a repository in an organization',
13
+ async run (argv, importMeta, { parentName }) {
14
+ const name = parentName + ' del'
15
+
16
+ const input = setupCommand(name, del.description, argv, importMeta)
17
+ if (input) {
18
+ const spinnerText = 'Deleting repository... \n'
19
+ const spinner = ora(spinnerText).start()
20
+ await deleteRepository(input.orgSlug, input.repoName, spinner)
21
+ }
22
+ }
23
+ }
24
+
25
+ // Internal functions
26
+
27
+ /**
28
+ * @typedef CommandContext
29
+ * @property {string} orgSlug
30
+ * @property {string} repoName
31
+ */
32
+
33
+ /**
34
+ * @param {string} name
35
+ * @param {string} description
36
+ * @param {readonly string[]} argv
37
+ * @param {ImportMeta} importMeta
38
+ * @returns {void|CommandContext}
39
+ */
40
+ function setupCommand (name, description, argv, importMeta) {
41
+ const cli = meow(`
42
+ Usage
43
+ $ ${name} <org slug> <repo slug>
44
+
45
+ Examples
46
+ $ ${name} FakeOrg test-repo
47
+ `, {
48
+ argv,
49
+ description,
50
+ importMeta
51
+ })
52
+
53
+ const [orgSlug = '', repoName = ''] = cli.input
54
+
55
+ if (!orgSlug || !repoName) {
56
+ console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug and repository slug \n`)
57
+ cli.showHelp()
58
+ return
59
+ }
60
+
61
+ return {
62
+ orgSlug,
63
+ repoName
64
+ }
65
+ }
66
+
67
+ /**
68
+ * @typedef RepositoryData
69
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'deleteOrgRepo'>["data"]} data
70
+ */
71
+
72
+ /**
73
+ * @param {string} orgSlug
74
+ * @param {string} repoName
75
+ * @param {import('ora').Ora} spinner
76
+ * @returns {Promise<void|RepositoryData>}
77
+ */
78
+ async function deleteRepository (orgSlug, repoName, spinner) {
79
+ const socketSdk = await setupSdk(getDefaultKey())
80
+ const result = await handleApiCall(socketSdk.deleteOrgRepo(orgSlug, repoName), 'deleting repository')
81
+
82
+ if (!result.success) {
83
+ return handleUnsuccessfulApiResponse('deleteOrgRepo', result, spinner)
84
+ }
85
+
86
+ spinner.stop()
87
+
88
+ console.log('\n✅ Repository deleted successfully \n')
89
+
90
+ return {
91
+ data: result.data
92
+ }
93
+ }
@@ -0,0 +1,30 @@
1
+ import { create } from './create.js'
2
+ import { del } from './delete.js'
3
+ import { list } from './list.js'
4
+ import { update } from './update.js'
5
+ import { view } from './view.js'
6
+ import { meowWithSubcommands } from '../../utils/meow-with-subcommands.js'
7
+
8
+ const description = 'Repositories related commands'
9
+
10
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
11
+ export const repo = {
12
+ description,
13
+ run: async (argv, importMeta, { parentName }) => {
14
+ await meowWithSubcommands(
15
+ {
16
+ create,
17
+ view,
18
+ list,
19
+ del,
20
+ update
21
+ },
22
+ {
23
+ argv,
24
+ description,
25
+ importMeta,
26
+ name: parentName + ' repo',
27
+ }
28
+ )
29
+ }
30
+ }
@@ -0,0 +1,170 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import chalk from 'chalk'
4
+ // @ts-ignore
5
+ import chalkTable from 'chalk-table'
6
+ import meow from 'meow'
7
+ import ora from 'ora'
8
+
9
+ import { outputFlags } from '../../flags/index.js'
10
+ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
11
+ import { prepareFlags } from '../../utils/flags.js'
12
+ import { printFlagList } from '../../utils/formatting.js'
13
+ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
14
+
15
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
16
+ export const list = {
17
+ description: 'List repositories in an organization',
18
+ async run (argv, importMeta, { parentName }) {
19
+ const name = parentName + ' list'
20
+
21
+ const input = setupCommand(name, list.description, argv, importMeta)
22
+ if (input) {
23
+ const spinnerText = 'Listing repositories... \n'
24
+ const spinner = ora(spinnerText).start()
25
+ await listOrgRepos(input.orgSlug, input, spinner)
26
+ }
27
+ }
28
+ }
29
+
30
+ const listRepoFlags = prepareFlags({
31
+ sort: {
32
+ type: 'string',
33
+ shortFlag: 's',
34
+ default: 'created_at',
35
+ description: 'Sorting option',
36
+ },
37
+ direction: {
38
+ type: 'string',
39
+ default: 'desc',
40
+ description: 'Direction option',
41
+ },
42
+ perPage: {
43
+ type: 'number',
44
+ shortFlag: 'pp',
45
+ default: 30,
46
+ description: 'Number of results per page'
47
+ },
48
+ page: {
49
+ type: 'number',
50
+ shortFlag: 'p',
51
+ default: 1,
52
+ description: 'Page number'
53
+ },
54
+ })
55
+
56
+ // Internal functions
57
+
58
+ /**
59
+ * @typedef CommandContext
60
+ * @property {boolean} outputJson
61
+ * @property {boolean} outputMarkdown
62
+ * @property {string} orgSlug
63
+ * @property {string} sort
64
+ * @property {string} direction
65
+ * @property {number} per_page
66
+ * @property {number} page
67
+ */
68
+
69
+ /**
70
+ * @param {string} name
71
+ * @param {string} description
72
+ * @param {readonly string[]} argv
73
+ * @param {ImportMeta} importMeta
74
+ * @returns {void|CommandContext}
75
+ */
76
+ function setupCommand (name, description, argv, importMeta) {
77
+ const flags = {
78
+ ...outputFlags,
79
+ ...listRepoFlags
80
+ }
81
+
82
+ const cli = meow(`
83
+ Usage
84
+ $ ${name} <org slug>
85
+
86
+ Options
87
+ ${printFlagList(flags, 6)}
88
+
89
+ Examples
90
+ $ ${name} FakeOrg
91
+ `, {
92
+ argv,
93
+ description,
94
+ importMeta,
95
+ flags
96
+ })
97
+
98
+ const {
99
+ json: outputJson,
100
+ markdown: outputMarkdown,
101
+ perPage,
102
+ sort,
103
+ direction,
104
+ page
105
+ } = cli.flags
106
+
107
+ if (!cli.input[0]) {
108
+ console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`)
109
+ cli.showHelp()
110
+ return
111
+ }
112
+
113
+ const [orgSlug = ''] = cli.input
114
+
115
+ return {
116
+ outputJson,
117
+ outputMarkdown,
118
+ orgSlug,
119
+ sort,
120
+ direction,
121
+ page,
122
+ per_page: perPage
123
+ }
124
+ }
125
+
126
+ /**
127
+ * @typedef RepositoryData
128
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getOrgRepoList'>["data"]} data
129
+ */
130
+
131
+ /**
132
+ * @param {string} orgSlug
133
+ * @param {CommandContext} input
134
+ * @param {import('ora').Ora} spinner
135
+ * @returns {Promise<void|RepositoryData>}
136
+ */
137
+ async function listOrgRepos (orgSlug, input, spinner) {
138
+ const socketSdk = await setupSdk(getDefaultKey())
139
+ const result = await handleApiCall(socketSdk.getOrgRepoList(orgSlug, input), 'listing repositories')
140
+
141
+ if (!result.success) {
142
+ return handleUnsuccessfulApiResponse('getOrgRepoList', result, spinner)
143
+ }
144
+
145
+ spinner.stop()
146
+
147
+ const options = {
148
+ columns: [
149
+ { field: 'id', name: chalk.magenta('ID') },
150
+ { field: 'name', name: chalk.magenta('Name') },
151
+ { field: 'visibility', name: chalk.magenta('Visibility') },
152
+ { field: 'default_branch', name: chalk.magenta('Default branch') },
153
+ { field: 'archived', name: chalk.magenta('Archived') }
154
+ ]
155
+ }
156
+
157
+ const formattedResults = result.data.results.map(d => {
158
+ return {
159
+ ...d
160
+ }
161
+ })
162
+
163
+ const table = chalkTable(options, formattedResults)
164
+
165
+ console.log(table, '\n')
166
+
167
+ return {
168
+ data: result.data
169
+ }
170
+ }
@@ -0,0 +1,166 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import chalk from 'chalk'
4
+ import meow from 'meow'
5
+ import ora from 'ora'
6
+
7
+ import { outputFlags } from '../../flags/index.js'
8
+ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
9
+ import { prepareFlags } from '../../utils/flags.js'
10
+ import { printFlagList } from '../../utils/formatting.js'
11
+ import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
12
+
13
+ /** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
14
+ export const update = {
15
+ description: 'Update a repository in an organization',
16
+ async run (argv, importMeta, { parentName }) {
17
+ const name = parentName + ' update'
18
+
19
+ const input = setupCommand(name, update.description, argv, importMeta)
20
+ if (input) {
21
+ const spinnerText = 'Updating repository... \n'
22
+ const spinner = ora(spinnerText).start()
23
+ await updateRepository(input.orgSlug, input, spinner)
24
+ }
25
+ }
26
+ }
27
+
28
+ const repositoryUpdateFlags = prepareFlags({
29
+ repoName: {
30
+ type: 'string',
31
+ shortFlag: 'n',
32
+ default: '',
33
+ description: 'Repository name',
34
+ },
35
+ repoDescription: {
36
+ type: 'string',
37
+ shortFlag: 'd',
38
+ default: '',
39
+ description: 'Repository description',
40
+ },
41
+ homepage: {
42
+ type: 'string',
43
+ shortFlag: 'h',
44
+ default: '',
45
+ description: 'Repository url',
46
+ },
47
+ defaultBranch: {
48
+ type: 'string',
49
+ shortFlag: 'b',
50
+ default: 'main',
51
+ description: 'Repository default branch',
52
+ },
53
+ visibility: {
54
+ type: 'string',
55
+ shortFlag: 'v',
56
+ default: 'private',
57
+ description: 'Repository visibility (Default Private)',
58
+ }
59
+ })
60
+
61
+ // Internal functions
62
+
63
+ /**
64
+ * @typedef CommandContext
65
+ * @property {boolean} outputJson
66
+ * @property {boolean} outputMarkdown
67
+ * @property {string} orgSlug
68
+ * @property {string} name
69
+ * @property {string} description
70
+ * @property {string} homepage
71
+ * @property {string} default_branch
72
+ * @property {string} visibility
73
+ */
74
+
75
+ /**
76
+ * @param {string} name
77
+ * @param {string} description
78
+ * @param {readonly string[]} argv
79
+ * @param {ImportMeta} importMeta
80
+ * @returns {void|CommandContext}
81
+ */
82
+ function setupCommand (name, description, argv, importMeta) {
83
+ const flags = {
84
+ ...outputFlags,
85
+ ...repositoryUpdateFlags
86
+ }
87
+
88
+ const cli = meow(`
89
+ Usage
90
+ $ ${name} <org slug>
91
+
92
+ Options
93
+ ${printFlagList(flags, 6)}
94
+
95
+ Examples
96
+ $ ${name} FakeOrg
97
+ `, {
98
+ argv,
99
+ description,
100
+ importMeta,
101
+ flags
102
+ })
103
+
104
+ const {
105
+ json: outputJson,
106
+ markdown: outputMarkdown,
107
+ repoName,
108
+ repoDescription,
109
+ homepage,
110
+ defaultBranch,
111
+ visibility
112
+ } = cli.flags
113
+
114
+ const [orgSlug = ''] = cli.input
115
+
116
+ if (!orgSlug) {
117
+ console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug and repository name \n`)
118
+ cli.showHelp()
119
+ return
120
+ }
121
+
122
+ if (!repoName) {
123
+ console.error(`${chalk.bgRed('Input error')}: Repository name is required. \n`)
124
+ cli.showHelp()
125
+ return
126
+ }
127
+
128
+ return {
129
+ outputJson,
130
+ outputMarkdown,
131
+ orgSlug,
132
+ name: repoName,
133
+ description: repoDescription,
134
+ homepage,
135
+ default_branch: defaultBranch,
136
+ visibility
137
+ }
138
+ }
139
+
140
+ /**
141
+ * @typedef RepositoryData
142
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'updateOrgRepo'>["data"]} data
143
+ */
144
+
145
+ /**
146
+ * @param {string} orgSlug
147
+ * @param {CommandContext} input
148
+ * @param {import('ora').Ora} spinner
149
+ * @returns {Promise<void|RepositoryData>}
150
+ */
151
+ async function updateRepository (orgSlug, input, spinner) {
152
+ const socketSdk = await setupSdk(getDefaultKey())
153
+ const result = await handleApiCall(socketSdk.updateOrgRepo(orgSlug, input.name, input), 'updating repository')
154
+
155
+ if (!result.success) {
156
+ return handleUnsuccessfulApiResponse('updateOrgRepo', result, spinner)
157
+ }
158
+
159
+ spinner.stop()
160
+
161
+ console.log('\n✅ Repository updated successfully \n')
162
+
163
+ return {
164
+ data: result.data
165
+ }
166
+ }