@shopify/cli-kit 3.34.0 → 3.35.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 (110) hide show
  1. package/dist/git.js +2 -2
  2. package/dist/git.js.map +1 -1
  3. package/dist/http/fetch.js +1 -1
  4. package/dist/http/fetch.js.map +1 -1
  5. package/dist/index.d.ts +0 -3
  6. package/dist/index.js +0 -3
  7. package/dist/index.js.map +1 -1
  8. package/dist/npm.js +3 -3
  9. package/dist/npm.js.map +1 -1
  10. package/dist/output.js +3 -3
  11. package/dist/output.js.map +1 -1
  12. package/dist/private/node/api/graphql.js +1 -1
  13. package/dist/private/node/api/graphql.js.map +1 -1
  14. package/dist/private/node/api/headers.d.ts +2 -2
  15. package/dist/private/node/api/headers.js +3 -3
  16. package/dist/private/node/api/headers.js.map +1 -1
  17. package/dist/private/node/api/rest.d.ts +5 -3
  18. package/dist/private/node/api/rest.js +8 -7
  19. package/dist/private/node/api/rest.js.map +1 -1
  20. package/dist/private/node/constants.d.ts +42 -0
  21. package/dist/private/node/constants.js +58 -0
  22. package/dist/private/node/constants.js.map +1 -0
  23. package/dist/private/node/environment/service.js +2 -2
  24. package/dist/private/node/environment/service.js.map +1 -1
  25. package/dist/private/node/session/post-auth.js +8 -8
  26. package/dist/private/node/session/post-auth.js.map +1 -1
  27. package/dist/private/node/session/store.js +6 -6
  28. package/dist/private/node/session/store.js.map +1 -1
  29. package/dist/private/node/session/validate.js +2 -2
  30. package/dist/private/node/session/validate.js.map +1 -1
  31. package/dist/private/node/session.js +2 -2
  32. package/dist/private/node/session.js.map +1 -1
  33. package/dist/private/node/ui/components/SelectPrompt.d.ts +2 -1
  34. package/dist/private/node/ui/components/SelectPrompt.js +2 -1
  35. package/dist/private/node/ui/components/SelectPrompt.js.map +1 -1
  36. package/dist/private/node/ui/components/Table/Column.d.ts +5 -0
  37. package/dist/private/node/ui/components/Table/Column.js +2 -0
  38. package/dist/private/node/ui/components/Table/Column.js.map +1 -0
  39. package/dist/private/node/ui/components/Table/Row.d.ts +12 -0
  40. package/dist/private/node/ui/components/Table/Row.js +24 -0
  41. package/dist/private/node/ui/components/Table/Row.js.map +1 -0
  42. package/dist/private/node/ui/components/Table/ScalarDict.d.ts +5 -0
  43. package/dist/private/node/ui/components/Table/ScalarDict.js +2 -0
  44. package/dist/private/node/ui/components/Table/ScalarDict.js.map +1 -0
  45. package/dist/private/node/ui/components/Table/Table.d.ts +12 -0
  46. package/dist/private/node/ui/components/Table/Table.js +30 -0
  47. package/dist/private/node/ui/components/Table/Table.js.map +1 -0
  48. package/dist/private/node/ui/components/Table/Table.test.d.ts +1 -0
  49. package/dist/private/node/ui/components/Table/Table.test.js +41 -0
  50. package/dist/private/node/ui/components/Table/Table.test.js.map +1 -0
  51. package/dist/public/common/string.d.ts +11 -0
  52. package/dist/public/common/string.js +21 -0
  53. package/dist/public/common/string.js.map +1 -1
  54. package/dist/public/common/version.d.ts +1 -0
  55. package/dist/public/common/version.js +2 -0
  56. package/dist/public/common/version.js.map +1 -0
  57. package/dist/public/node/analytics.js +2 -2
  58. package/dist/public/node/analytics.js.map +1 -1
  59. package/dist/public/node/api/admin.d.ts +4 -1
  60. package/dist/public/node/api/admin.js +4 -3
  61. package/dist/public/node/api/admin.js.map +1 -1
  62. package/dist/public/node/cli.js +4 -4
  63. package/dist/public/node/cli.js.map +1 -1
  64. package/dist/public/node/dot-env.js +2 -2
  65. package/dist/public/node/dot-env.js.map +1 -1
  66. package/dist/public/node/environment/local.js +17 -17
  67. package/dist/public/node/environment/local.js.map +1 -1
  68. package/dist/public/node/environment/spin.js +5 -5
  69. package/dist/public/node/environment/spin.js.map +1 -1
  70. package/dist/public/node/error-handler.js +6 -5
  71. package/dist/public/node/error-handler.js.map +1 -1
  72. package/dist/public/node/fs.d.ts +222 -3
  73. package/dist/public/node/fs.js +338 -2
  74. package/dist/public/node/fs.js.map +1 -1
  75. package/dist/public/node/git.d.ts +90 -0
  76. package/dist/public/node/git.js +174 -0
  77. package/dist/public/node/git.js.map +1 -0
  78. package/dist/public/node/liquid.js +6 -6
  79. package/dist/public/node/liquid.js.map +1 -1
  80. package/dist/public/node/node-package-manager.d.ts +0 -10
  81. package/dist/public/node/node-package-manager.js +2 -19
  82. package/dist/public/node/node-package-manager.js.map +1 -1
  83. package/dist/public/node/presets.js +2 -2
  84. package/dist/public/node/presets.js.map +1 -1
  85. package/dist/public/node/ruby.js +8 -8
  86. package/dist/public/node/ruby.js.map +1 -1
  87. package/dist/public/node/session.js +2 -2
  88. package/dist/public/node/session.js.map +1 -1
  89. package/dist/public/node/ui.d.ts +13 -0
  90. package/dist/public/node/ui.js +14 -0
  91. package/dist/public/node/ui.js.map +1 -1
  92. package/dist/public/node/vscode.js +5 -5
  93. package/dist/public/node/vscode.js.map +1 -1
  94. package/dist/secure-store.js +4 -4
  95. package/dist/secure-store.js.map +1 -1
  96. package/dist/store.d.ts +10 -10
  97. package/dist/store.js +21 -22
  98. package/dist/store.js.map +1 -1
  99. package/dist/testing/store.js +3 -3
  100. package/dist/testing/store.js.map +1 -1
  101. package/dist/tsconfig.tsbuildinfo +1 -1
  102. package/dist/ui.js +3 -3
  103. package/dist/ui.js.map +1 -1
  104. package/package.json +2 -2
  105. package/dist/constants.d.ts +0 -48
  106. package/dist/constants.js +0 -67
  107. package/dist/constants.js.map +0 -1
  108. package/dist/file.d.ts +0 -98
  109. package/dist/file.js +0 -216
  110. package/dist/file.js.map +0 -1
@@ -1,5 +1,5 @@
1
1
  import { findUp, moduleDirectory } from '../../../path.js';
2
- import { read } from '../../../file.js';
2
+ import { readFile } from '../../../public/node/fs.js';
3
3
  import { Bug } from '../../../error.js';
4
4
  const HTMLFileNames = [
5
5
  'empty-url.html',
@@ -28,31 +28,31 @@ const getFilePath = async (fileName) => {
28
28
  };
29
29
  export const getEmptyUrlHTML = async () => {
30
30
  const filePath = await getFilePath(HTMLFileNames[0]);
31
- return read(filePath);
31
+ return readFile(filePath);
32
32
  };
33
33
  export const getAuthErrorHTML = async () => {
34
34
  const filePath = await getFilePath(HTMLFileNames[1]);
35
- return read(filePath);
35
+ return readFile(filePath);
36
36
  };
37
37
  export const getMissingCodeHTML = async () => {
38
38
  const filePath = await getFilePath(HTMLFileNames[2]);
39
- return read(filePath);
39
+ return readFile(filePath);
40
40
  };
41
41
  export const getMissingStateHTML = async () => {
42
42
  const filePath = await getFilePath(HTMLFileNames[3]);
43
- return read(filePath);
43
+ return readFile(filePath);
44
44
  };
45
45
  export const getSuccessHTML = async () => {
46
46
  const filePath = await getFilePath(HTMLFileNames[4]);
47
- return read(filePath);
47
+ return readFile(filePath);
48
48
  };
49
49
  export const getStylesheet = async () => {
50
50
  const filePath = await getFilePath(StylesheetFilename);
51
- return read(filePath);
51
+ return readFile(filePath);
52
52
  };
53
53
  export const getFavicon = async () => {
54
54
  const filePath = await getFilePath(FaviconFileName);
55
- return read(filePath);
55
+ return readFile(filePath);
56
56
  };
57
57
  export const EmptyUrlString = 'We received the authentication redirect but the URL is empty.';
58
58
  export const AuthErrorString = 'There was an issue while trying to authenticate.';
@@ -1 +1 @@
1
- {"version":3,"file":"post-auth.js","sourceRoot":"","sources":["../../../../src/private/node/session/post-auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAE,eAAe,EAAC,MAAM,kBAAkB,CAAA;AACxD,OAAO,EAAC,IAAI,EAAC,MAAM,kBAAkB,CAAA;AACrC,OAAO,EAAC,GAAG,EAAC,MAAM,mBAAmB,CAAA;AAErC,MAAM,aAAa,GAAG;IACpB,gBAAgB;IAChB,iBAAiB;IACjB,mBAAmB;IACnB,oBAAoB;IACpB,cAAc;CACN,CAAA;AACV,MAAM,kBAAkB,GAAG,WAAW,CAAA;AACtC,MAAM,eAAe,GAAG,aAAa,CAAA;AAErC;;;;;GAKG;AACH,MAAM,WAAW,GAAG,KAAK,EAAE,QAAgB,EAAmB,EAAE;IAC9D,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,UAAU,QAAQ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,MAAM,aAAa,GAAG,KAAK,IAAqB,EAAE;IACvD,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,kBAAkB,CAAC,CAAA;IACtD,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAA;AACvB,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,IAAqB,EAAE;IACpD,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,eAAe,CAAC,CAAA;IACnD,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAA;AACvB,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,+DAA+D,CAAA;AAE7F,MAAM,CAAC,MAAM,eAAe,GAAG,kDAAkD,CAAA;AAEjF,MAAM,CAAC,MAAM,iBAAiB,GAAG,kFAAkF,CAAA;AAEnH,MAAM,CAAC,MAAM,kBAAkB,GAAG,mFAAmF,CAAA;AAErH,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,EAAE,CAAC,IAAI,GAAG,CAAC,+BAA+B,CAAC,CAAA","sourcesContent":["import {findUp, moduleDirectory} from '../../../path.js'\nimport {read} from '../../../file.js'\nimport {Bug} from '../../../error.js'\n\nconst HTMLFileNames = [\n 'empty-url.html',\n 'auth-error.html',\n 'missing-code.html',\n 'missing-state.html',\n 'success.html',\n] as const\nconst StylesheetFilename = 'style.css'\nconst FaviconFileName = 'favicon.svg'\n\n/**\n * Finds the full path of the given file-name from the assets folder.\n *\n * @param fileName - The name of the file to look for.\n * @returns The full path of the file, or null if not found.\n */\nconst getFilePath = async (fileName: string): Promise<string> => {\n const filePath = await findUp(`assets/${fileName}`, {\n type: 'file',\n cwd: moduleDirectory(import.meta.url),\n })\n if (!filePath) {\n throw RedirectPageAssetNotFoundError()\n }\n return filePath\n}\n\nexport const getEmptyUrlHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[0])\n return read(filePath)\n}\n\nexport const getAuthErrorHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[1])\n return read(filePath)\n}\n\nexport const getMissingCodeHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[2])\n return read(filePath)\n}\n\nexport const getMissingStateHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[3])\n return read(filePath)\n}\n\nexport const getSuccessHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[4])\n return read(filePath)\n}\n\nexport const getStylesheet = async (): Promise<string> => {\n const filePath = await getFilePath(StylesheetFilename)\n return read(filePath)\n}\n\nexport const getFavicon = async (): Promise<string> => {\n const filePath = await getFilePath(FaviconFileName)\n return read(filePath)\n}\n\nexport const EmptyUrlString = 'We received the authentication redirect but the URL is empty.'\n\nexport const AuthErrorString = 'There was an issue while trying to authenticate.'\n\nexport const MissingCodeString = \"The authentication can't continue because the redirect doesn't include the code.\"\n\nexport const MissingStateString = \"The authentication can't continue because the redirect doesn't include the state.\"\n\nexport const RedirectPageAssetNotFoundError = () => new Bug(`Redirect page asset not found`)\n"]}
1
+ {"version":3,"file":"post-auth.js","sourceRoot":"","sources":["../../../../src/private/node/session/post-auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAE,eAAe,EAAC,MAAM,kBAAkB,CAAA;AACxD,OAAO,EAAC,QAAQ,EAAC,MAAM,4BAA4B,CAAA;AACnD,OAAO,EAAC,GAAG,EAAC,MAAM,mBAAmB,CAAA;AAErC,MAAM,aAAa,GAAG;IACpB,gBAAgB;IAChB,iBAAiB;IACjB,mBAAmB;IACnB,oBAAoB;IACpB,cAAc;CACN,CAAA;AACV,MAAM,kBAAkB,GAAG,WAAW,CAAA;AACtC,MAAM,eAAe,GAAG,aAAa,CAAA;AAErC;;;;;GAKG;AACH,MAAM,WAAW,GAAG,KAAK,EAAE,QAAgB,EAAmB,EAAE;IAC9D,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,UAAU,QAAQ,EAAE,EAAE;QAClD,IAAI,EAAE,MAAM;QACZ,GAAG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E;IACvD,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,kBAAkB,CAAC,CAAA;IACtD,OAAO,QAAQ,CAAC,QAAQ,CAAC,CAAA;AAC3B,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,IAAqB,EAAE;IACpD,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,eAAe,CAAC,CAAA;IACnD,OAAO,QAAQ,CAAC,QAAQ,CAAC,CAAA;AAC3B,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,+DAA+D,CAAA;AAE7F,MAAM,CAAC,MAAM,eAAe,GAAG,kDAAkD,CAAA;AAEjF,MAAM,CAAC,MAAM,iBAAiB,GAAG,kFAAkF,CAAA;AAEnH,MAAM,CAAC,MAAM,kBAAkB,GAAG,mFAAmF,CAAA;AAErH,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,EAAE,CAAC,IAAI,GAAG,CAAC,+BAA+B,CAAC,CAAA","sourcesContent":["import {findUp, moduleDirectory} from '../../../path.js'\nimport {readFile} from '../../../public/node/fs.js'\nimport {Bug} from '../../../error.js'\n\nconst HTMLFileNames = [\n 'empty-url.html',\n 'auth-error.html',\n 'missing-code.html',\n 'missing-state.html',\n 'success.html',\n] as const\nconst StylesheetFilename = 'style.css'\nconst FaviconFileName = 'favicon.svg'\n\n/**\n * Finds the full path of the given file-name from the assets folder.\n *\n * @param fileName - The name of the file to look for.\n * @returns The full path of the file, or null if not found.\n */\nconst getFilePath = async (fileName: string): Promise<string> => {\n const filePath = await findUp(`assets/${fileName}`, {\n type: 'file',\n cwd: moduleDirectory(import.meta.url),\n })\n if (!filePath) {\n throw RedirectPageAssetNotFoundError()\n }\n return filePath\n}\n\nexport const getEmptyUrlHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[0])\n return readFile(filePath)\n}\n\nexport const getAuthErrorHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[1])\n return readFile(filePath)\n}\n\nexport const getMissingCodeHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[2])\n return readFile(filePath)\n}\n\nexport const getMissingStateHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[3])\n return readFile(filePath)\n}\n\nexport const getSuccessHTML = async (): Promise<string> => {\n const filePath = await getFilePath(HTMLFileNames[4])\n return readFile(filePath)\n}\n\nexport const getStylesheet = async (): Promise<string> => {\n const filePath = await getFilePath(StylesheetFilename)\n return readFile(filePath)\n}\n\nexport const getFavicon = async (): Promise<string> => {\n const filePath = await getFilePath(FaviconFileName)\n return readFile(filePath)\n}\n\nexport const EmptyUrlString = 'We received the authentication redirect but the URL is empty.'\n\nexport const AuthErrorString = 'There was an issue while trying to authenticate.'\n\nexport const MissingCodeString = \"The authentication can't continue because the redirect doesn't include the code.\"\n\nexport const MissingStateString = \"The authentication can't continue because the redirect doesn't include the state.\"\n\nexport const RedirectPageAssetNotFoundError = () => new Bug(`Redirect page asset not found`)\n"]}
@@ -1,5 +1,5 @@
1
1
  import { SessionSchema } from './schema.js';
2
- import constants from '../../../constants.js';
2
+ import { keychainConstants } from '../constants.js';
3
3
  import { platformAndArch } from '../../../public/node/os.js';
4
4
  import { store as secureStore, fetch as secureFetch, remove as secureRemove } from '../../../secure-store.js';
5
5
  import { content, debug } from '../../../output.js';
@@ -19,7 +19,7 @@ export async function store(session) {
19
19
  await secureStore(identifier, jsonSession);
20
20
  }
21
21
  else {
22
- await setSession(jsonSession);
22
+ setSession(jsonSession);
23
23
  }
24
24
  }
25
25
  /**
@@ -36,7 +36,7 @@ export async function fetch() {
36
36
  content = await secureFetch(identifier);
37
37
  }
38
38
  else {
39
- content = await getSession();
39
+ content = getSession();
40
40
  }
41
41
  if (!content) {
42
42
  return undefined;
@@ -59,9 +59,9 @@ export async function remove() {
59
59
  await secureRemove(identifier);
60
60
  }
61
61
  else {
62
- await removeSession();
62
+ removeSession();
63
63
  }
64
- await clearAllAppInfo();
64
+ clearAllAppInfo();
65
65
  }
66
66
  /**
67
67
  * Returns true if the secure store is available on the system.
@@ -76,7 +76,7 @@ async function secureStoreAvailable() {
76
76
  return false;
77
77
  }
78
78
  const keytar = await import('keytar');
79
- await keytar.default.findCredentials(constants.keychain.service);
79
+ await keytar.default.findCredentials(keychainConstants.service);
80
80
  debug(content `Secure store is available`);
81
81
  return true;
82
82
  // eslint-disable-next-line no-catch-all/no-catch-all
@@ -1 +1 @@
1
- {"version":3,"file":"store.js","sourceRoot":"","sources":["../../../../src/private/node/session/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,aAAa,CAAA;AACzC,OAAO,SAAS,MAAM,uBAAuB,CAAA;AAC7C,OAAO,EAAC,eAAe,EAAC,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAC,KAAK,IAAI,WAAW,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,IAAI,YAAY,EAAC,MAAM,0BAA0B,CAAA;AAC3G,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAC,UAAU,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,EAAC,MAAM,mBAAmB,CAAA;AAGxF;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAA;AAEnC;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,OAAgB;IAC1C,MAAM,WAAW,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;IAC3C,IAAI,MAAM,oBAAoB,EAAE,EAAE;QAChC,MAAM,WAAW,CAAC,UAAU,EAAE,WAAW,CAAC,CAAA;KAC3C;SAAM;QACL,MAAM,UAAU,CAAC,WAAW,CAAC,CAAA;KAC9B;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK;IACzB,IAAI,OAAO,CAAA;IACX,IAAI,MAAM,oBAAoB,EAAE,EAAE;QAChC,O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import {SessionSchema} from './schema.js'\nimport constants from '../../../constants.js'\nimport {platformAndArch} from '../../../public/node/os.js'\nimport {store as secureStore, fetch as secureFetch, remove as secureRemove} from '../../../secure-store.js'\nimport {content, debug} from '../../../output.js'\nimport {getSession, removeSession, setSession, clearAllAppInfo} from '../../../store.js'\nimport type {Session} from './schema.js'\n\n/**\n * The identifier of the session in the secure store.\n */\nexport const identifier = 'session'\n\n/**\n * Serializes the session as a JSON and stores it securely in the system.\n * If the secure store is not available, the session is stored in the local config.\n * @param session - the session to store.\n */\nexport async function store(session: Session) {\n const jsonSession = JSON.stringify(session)\n if (await secureStoreAvailable()) {\n await secureStore(identifier, jsonSession)\n } else {\n await setSession(jsonSession)\n }\n}\n\n/**\n * Fetches the session from the secure store and returns it.\n * If the secure store is not available, the session is fetched from the local config.\n * If the format of the session is invalid, the method will discard it.\n * In the future might add some logic for supporting migrating the schema\n * of already-persisted sessions.\n * @returns Returns a promise that resolves with the session if it exists and is valid.\n */\nexport async function fetch(): Promise<Session | undefined> {\n let content\n if (await secureStoreAvailable()) {\n content = await secureFetch(identifier)\n } else {\n content = await getSession()\n }\n\n if (!content) {\n return undefined\n }\n const contentJson = JSON.parse(content)\n const parsedSession = await SessionSchema.safeParseAsync(contentJson)\n if (parsedSession.success) {\n return parsedSession.data\n } else {\n await remove()\n return undefined\n }\n}\n\n/**\n * Removes a session from the system.\n */\nexport async function remove() {\n if (await secureStoreAvailable()) {\n await secureRemove(identifier)\n } else {\n await removeSession()\n }\n\n await clearAllAppInfo()\n}\n\n/**\n * Returns true if the secure store is available on the system.\n * Keytar it's not supported on some Linux environments or Windows.\n * More details: https://github.com/Shopify/shopify-cli-planning/issues/261\n * @returns a boolean indicating if the secure store is available.\n */\nasync function secureStoreAvailable(): Promise<boolean> {\n try {\n if (platformAndArch().platform === 'windows') {\n debug(content`Secure store not supported on Windows`)\n return false\n }\n const keytar = await import('keytar')\n await keytar.default.findCredentials(constants.keychain.service)\n debug(content`Secure store is available`)\n return true\n // eslint-disable-next-line no-catch-all/no-catch-all\n } catch (_error) {\n debug(content`Failed to load secure store`)\n return false\n }\n}\n"]}
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../../../../src/private/node/session/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,aAAa,CAAA;AACzC,OAAO,EAAC,iBAAiB,EAAC,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAC,eAAe,EAAC,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAC,KAAK,IAAI,WAAW,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,IAAI,YAAY,EAAC,MAAM,0BAA0B,CAAA;AAC3G,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAC,UAAU,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,EAAC,MAAM,mBAAmB,CAAA;AAGxF;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAA;AAEnC;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,OAAgB;IAC1C,MAAM,WAAW,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;IAC3C,IAAI,MAAM,oBAAoB,EAAE,EAAE;QAChC,MAAM,WAAW,CAAC,UAAU,EAAE,WAAW,CAAC,CAAA;KAC3C;SAAM;QACL,UAAU,CAAC,WAAW,CAAC,CAAA;KACxB;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK;IACzB,IAAI,OAAO,CAAA;IACX,IAAI,MAAM,oBAAoB,EAAE,EAAE;QAChC,OAAO,GAAG,MAAM,WAAW,CAAC,UAAU,CAAC,CAAA;KACxC;SAAM;QACL,OAAO,GAAG,UAAU,EAAE,CAAA;KACvB;IAED,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,SAAS,CAAA;KACjB;IACD,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;IACvC,MAAM,aAAa,GAAG,MAAM,aAAa,CAAC,cAAc,CAAC,WAAW,CAAC,CAAA;IACrE,IAAI,aAAa,CAAC,OAAO,EAAE;QACzB,OAAO,aAAa,CAAC,IAAI,CAAA;KAC1B;SAAM;QACL,MAAM,MAAM,EAAE,CAAA;QACd,OAAO,SAAS,CAAA;KACjB;AACH,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM;IAC1B,IAAI,MAAM,oBAAoB,EAAE,EAAE;QAChC,MAAM,YAAY,CAAC,UAAU,CAAC,CAAA;KAC/B;SAAM;QACL,aAAa,EAAE,CAAA;KAChB;IAED,eAAe,EAAE,CAAA;AACnB,CAAC;AAED;;;;;GAKG;AACH,KAAK,UAAU,oBAAoB;IACjC,IAAI;QACF,IAAI,eAAe,EAAE,CAAC,QAAQ,KAAK,SAAS,EAAE;YAC5C,KAAK,CAAC,OAAO,CAAA,uCAAuC,CAAC,CAAA;YACrD,OAAO,KAAK,CAAA;SACb;QACD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,CAAA;QACrC,MAAM,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAA;QAC/D,KAAK,CAAC,OAAO,CAAA,2BAA2B,CAAC,CAAA;QACzC,OAAO,IAAI,CAAA;QACX,qDAAqD;KACtD;IAAC,OAAO,MAAM,EAAE;QACf,KAAK,CAAC,OAAO,CAAA,6BAA6B,CAAC,CAAA;QAC3C,OAAO,KAAK,CAAA;KACb;AACH,CAAC","sourcesContent":["import {SessionSchema} from './schema.js'\nimport {keychainConstants} from '../constants.js'\nimport {platformAndArch} from '../../../public/node/os.js'\nimport {store as secureStore, fetch as secureFetch, remove as secureRemove} from '../../../secure-store.js'\nimport {content, debug} from '../../../output.js'\nimport {getSession, removeSession, setSession, clearAllAppInfo} from '../../../store.js'\nimport type {Session} from './schema.js'\n\n/**\n * The identifier of the session in the secure store.\n */\nexport const identifier = 'session'\n\n/**\n * Serializes the session as a JSON and stores it securely in the system.\n * If the secure store is not available, the session is stored in the local config.\n * @param session - the session to store.\n */\nexport async function store(session: Session) {\n const jsonSession = JSON.stringify(session)\n if (await secureStoreAvailable()) {\n await secureStore(identifier, jsonSession)\n } else {\n setSession(jsonSession)\n }\n}\n\n/**\n * Fetches the session from the secure store and returns it.\n * If the secure store is not available, the session is fetched from the local config.\n * If the format of the session is invalid, the method will discard it.\n * In the future might add some logic for supporting migrating the schema\n * of already-persisted sessions.\n * @returns Returns a promise that resolves with the session if it exists and is valid.\n */\nexport async function fetch(): Promise<Session | undefined> {\n let content\n if (await secureStoreAvailable()) {\n content = await secureFetch(identifier)\n } else {\n content = getSession()\n }\n\n if (!content) {\n return undefined\n }\n const contentJson = JSON.parse(content)\n const parsedSession = await SessionSchema.safeParseAsync(contentJson)\n if (parsedSession.success) {\n return parsedSession.data\n } else {\n await remove()\n return undefined\n }\n}\n\n/**\n * Removes a session from the system.\n */\nexport async function remove() {\n if (await secureStoreAvailable()) {\n await secureRemove(identifier)\n } else {\n removeSession()\n }\n\n clearAllAppInfo()\n}\n\n/**\n * Returns true if the secure store is available on the system.\n * Keytar it's not supported on some Linux environments or Windows.\n * More details: https://github.com/Shopify/shopify-cli-planning/issues/261\n * @returns a boolean indicating if the secure store is available.\n */\nasync function secureStoreAvailable(): Promise<boolean> {\n try {\n if (platformAndArch().platform === 'windows') {\n debug(content`Secure store not supported on Windows`)\n return false\n }\n const keytar = await import('keytar')\n await keytar.default.findCredentials(keychainConstants.service)\n debug(content`Secure store is available`)\n return true\n // eslint-disable-next-line no-catch-all/no-catch-all\n } catch (_error) {\n debug(content`Failed to load secure store`)\n return false\n }\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  import { applicationId } from './identity.js';
2
2
  import { validateIdentityToken } from './identity-token-validation.js';
3
- import constants from '../../../constants.js';
3
+ import { sessionConstants } from '../constants.js';
4
4
  import { debug } from '../../../output.js';
5
5
  import { firstPartyDev } from '../../../public/node/environment/local.js';
6
6
  /**
@@ -60,6 +60,6 @@ function isTokenExpired(token) {
60
60
  return token.expiresAt < expireThreshold();
61
61
  }
62
62
  function expireThreshold() {
63
- return new Date(Date.now() + constants.session.expirationTimeMarginInMinutes * 60 * 1000);
63
+ return new Date(Date.now() + sessionConstants.expirationTimeMarginInMinutes * 60 * 1000);
64
64
  }
65
65
  //# sourceMappingURL=validate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"validate.js","sourceRoot":"","sources":["../../../../src/private/node/session/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,eAAe,CAAA;AAE3C,OAAO,EAAC,qBAAqB,EAAC,MAAM,gCAAgC,CAAA;AACpE,OAAO,SAAS,MAAM,uBAAuB,CAAA;AAC7C,OAAO,EAAC,KAAK,EAAC,MAAM,oBAAoB,CAAA;AACxC,OAAO,EAAC,aAAa,EAAC,MAAM,2CAA2C,CAAA;AAKvE;;GAEG;AACH,SAAS,cAAc,CAAC,eAAyB,EAAE,QAAuB;IACxE,MAAM,aAAa,GAAG,QAAQ,CAAC,MAAM,CAAA;IACrC,IAAI,aAAa,EAAE,KAAK,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC;QAAE,OAAO,KAAK,CAAA;IACxE,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;AACxE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,MAAgB,EAChB,YAA+B,EAC/B,OAGC;IAED,IAAI,CAAC,OAAO;QAAE,OAAO,iBAAiB,CAAA;IACtC,MAAM,cAAc,GAAG,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA;IAC/D,MAAM,eAAe,GAAG,MAAM,qBAAqB,CAAC,OAAO,CAAC,QAAQ,C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C,SAAS,GAAG,eAAe,EAAE,CAAA;AAC5C,CAAC;AAED,SAAS,eAAe;IACtB,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,OAAO,CAAC,6BAA6B,GAAG,EAAE,GAAG,IAAI,CAAC,CAAA;AAC3F,CAAC","sourcesContent":["import {applicationId} from './identity.js'\nimport {ApplicationToken, IdentityToken} from './schema.js'\nimport {validateIdentityToken} from './identity-token-validation.js'\nimport constants from '../../../constants.js'\nimport {debug} from '../../../output.js'\nimport {firstPartyDev} from '../../../public/node/environment/local.js'\nimport {OAuthApplications} from '../session.js'\n\ntype ValidationResult = 'needs_refresh' | 'needs_full_auth' | 'ok'\n\n/**\n * Validate if an identity token is valid for the requested scopes\n */\nfunction validateScopes(requestedScopes: string[], identity: IdentityToken) {\n const currentScopes = identity.scopes\n if (firstPartyDev() !== currentScopes.includes('employee')) return false\n return requestedScopes.every((scope) => currentScopes.includes(scope))\n}\n\n/**\n * Validate if the current session is valid or we need to refresh/re-authenticate\n * @param scopes - requested scopes to validate\n * @param applications - requested applications\n * @param session - current session with identity and application tokens\n * @returns 'ok' if the session is valid, 'needs_full_auth' if we need to re-authenticate, 'needs_refresh' if we need to refresh the session\n */\nexport async function validateSession(\n scopes: string[],\n applications: OAuthApplications,\n session: {\n identity: IdentityToken\n applications: {[x: string]: ApplicationToken}\n },\n): Promise<ValidationResult> {\n if (!session) return 'needs_full_auth'\n const scopesAreValid = validateScopes(scopes, session.identity)\n const identityIsValid = await validateIdentityToken(session.identity.accessToken)\n if (!scopesAreValid) return 'needs_full_auth'\n let tokensAreExpired = isTokenExpired(session.identity)\n\n if (applications.partnersApi) {\n const appId = applicationId('partners')\n const token = session.applications[appId]!\n tokensAreExpired = tokensAreExpired || isTokenExpired(token)\n }\n\n if (applications.storefrontRendererApi) {\n const appId = applicationId('storefront-renderer')\n const token = session.applications[appId]!\n tokensAreExpired = tokensAreExpired || isTokenExpired(token)\n }\n\n if (applications.adminApi) {\n const appId = applicationId('admin')\n const realAppId = `${applications.adminApi.storeFqdn}-${appId}`\n const token = session.applications[realAppId]!\n tokensAreExpired = tokensAreExpired || isTokenExpired(token)\n }\n\n debug(`\nThe validation of the token for application/identity completed with the following results:\n- It's expired: ${tokensAreExpired}\n- It's invalid in identity: ${!identityIsValid}\n `)\n\n if (tokensAreExpired) return 'needs_refresh'\n if (!identityIsValid) return 'needs_full_auth'\n return 'ok'\n}\n\nfunction isTokenExpired(token: ApplicationToken): boolean {\n if (!token) return true\n return token.expiresAt < expireThreshold()\n}\n\nfunction expireThreshold(): Date {\n return new Date(Date.now() + constants.session.expirationTimeMarginInMinutes * 60 * 1000)\n}\n"]}
1
+ {"version":3,"file":"validate.js","sourceRoot":"","sources":["../../../../src/private/node/session/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,eAAe,CAAA;AAE3C,OAAO,EAAC,qBAAqB,EAAC,MAAM,gCAAgC,CAAA;AACpE,OAAO,EAAC,gBAAgB,EAAC,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAC,KAAK,EAAC,MAAM,oBAAoB,CAAA;AACxC,OAAO,EAAC,aAAa,EAAC,MAAM,2CAA2C,CAAA;AAKvE;;GAEG;AACH,SAAS,cAAc,CAAC,eAAyB,EAAE,QAAuB;IACxE,MAAM,aAAa,GAAG,QAAQ,CAAC,MAAM,CAAA;IACrC,IAAI,aAAa,EAAE,KAAK,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC;QAAE,OAAO,KAAK,CAAA;IACxE,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;AACxE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,MAAgB,EAChB,YAA+B,EAC/B,OAGC;IAED,IAAI,CAAC,OAAO;QAAE,OAAO,iBAAiB,CAAA;I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,CAAA;IAC9C,OAAO,IAAI,CAAA;AACb,CAAC;AAED,SAAS,cAAc,CAAC,KAAuB;IAC7C,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAA;IACvB,OAAO,KAAK,CAAC,SAAS,GAAG,eAAe,EAAE,CAAA;AAC5C,CAAC;AAED,SAAS,eAAe;IACtB,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,gBAAgB,CAAC,6BAA6B,GAAG,EAAE,GAAG,IAAI,CAAC,CAAA;AAC1F,CAAC","sourcesContent":["import {applicationId} from './identity.js'\nimport {ApplicationToken, IdentityToken} from './schema.js'\nimport {validateIdentityToken} from './identity-token-validation.js'\nimport {sessionConstants} from '../constants.js'\nimport {debug} from '../../../output.js'\nimport {firstPartyDev} from '../../../public/node/environment/local.js'\nimport {OAuthApplications} from '../session.js'\n\ntype ValidationResult = 'needs_refresh' | 'needs_full_auth' | 'ok'\n\n/**\n * Validate if an identity token is valid for the requested scopes\n */\nfunction validateScopes(requestedScopes: string[], identity: IdentityToken) {\n const currentScopes = identity.scopes\n if (firstPartyDev() !== currentScopes.includes('employee')) return false\n return requestedScopes.every((scope) => currentScopes.includes(scope))\n}\n\n/**\n * Validate if the current session is valid or we need to refresh/re-authenticate\n * @param scopes - requested scopes to validate\n * @param applications - requested applications\n * @param session - current session with identity and application tokens\n * @returns 'ok' if the session is valid, 'needs_full_auth' if we need to re-authenticate, 'needs_refresh' if we need to refresh the session\n */\nexport async function validateSession(\n scopes: string[],\n applications: OAuthApplications,\n session: {\n identity: IdentityToken\n applications: {[x: string]: ApplicationToken}\n },\n): Promise<ValidationResult> {\n if (!session) return 'needs_full_auth'\n const scopesAreValid = validateScopes(scopes, session.identity)\n const identityIsValid = await validateIdentityToken(session.identity.accessToken)\n if (!scopesAreValid) return 'needs_full_auth'\n let tokensAreExpired = isTokenExpired(session.identity)\n\n if (applications.partnersApi) {\n const appId = applicationId('partners')\n const token = session.applications[appId]!\n tokensAreExpired = tokensAreExpired || isTokenExpired(token)\n }\n\n if (applications.storefrontRendererApi) {\n const appId = applicationId('storefront-renderer')\n const token = session.applications[appId]!\n tokensAreExpired = tokensAreExpired || isTokenExpired(token)\n }\n\n if (applications.adminApi) {\n const appId = applicationId('admin')\n const realAppId = `${applications.adminApi.storeFqdn}-${appId}`\n const token = session.applications[realAppId]!\n tokensAreExpired = tokensAreExpired || isTokenExpired(token)\n }\n\n debug(`\nThe validation of the token for application/identity completed with the following results:\n- It's expired: ${tokensAreExpired}\n- It's invalid in identity: ${!identityIsValid}\n `)\n\n if (tokensAreExpired) return 'needs_refresh'\n if (!identityIsValid) return 'needs_full_auth'\n return 'ok'\n}\n\nfunction isTokenExpired(token: ApplicationToken): boolean {\n if (!token) return true\n return token.expiresAt < expireThreshold()\n}\n\nfunction expireThreshold(): Date {\n return new Date(Date.now() + sessionConstants.expirationTimeMarginInMinutes * 60 * 1000)\n}\n"]}
@@ -6,9 +6,9 @@ import { authorize } from './session/authorize.js';
6
6
  import * as secureStore from './session/store.js';
7
7
  import { pollForDeviceAuthorization, requestDeviceAuthorization } from './session/device-authorization.js';
8
8
  import { RequestClientError } from './api/headers.js';
9
+ import { environmentVariables } from './constants.js';
9
10
  import { content, token, debug } from '../../output.js';
10
11
  import { keypress } from '../../ui.js';
11
- import constants from '../../constants.js';
12
12
  import * as output from '../../output.js';
13
13
  import { firstPartyDev, useDeviceAuth } from '../../public/node/environment/local.js';
14
14
  import { AbortError } from '../../public/node/error.js';
@@ -70,7 +70,7 @@ ${token.json(applications)}
70
70
  await secureStore.store(completeSession);
71
71
  const tokens = await tokensFor(applications, completeSession, fqdn);
72
72
  // Overwrite partners token if using a custom CLI Token
73
- const envToken = env[constants.environmentVariables.partnersToken];
73
+ const envToken = env[environmentVariables.partnersToken];
74
74
  if (envToken && applications.partnersApi) {
75
75
  tokens.partners = (await exchangeCustomPartnerToken(envToken)).accessToken;
76
76
  }
@@ -1 +1 @@
1
- {"version":3,"file":"session.js","sourceRoot":"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,CAAA;AACf,CAAC;AAED,gBAAgB;AAChB;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,IAAuB;IAC/C,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,CAAA;IACzC,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,EAAE,MAAM,IAAI,EAAE,CAAA;IAC9C,MAAM,UAAU,GAAG,IAAI,CAAC,qBAAqB,EAAE,MAAM,IAAI,EAAE,CAAA;IAC3D,MAAM,eAAe,GAAG,CAAC,GAAG,KAAK,EAAE,GAAG,OAAO,EAAE,GAAG,UAAU,CAAC,CAAA;IAC7D,OAAO,gBAAgB,CAAC,eAAe,CAAC,CAAA;AAC1C,CAAC;AAED;;;;;GAKG;AACH,SAAS,iBAAiB,CAAC,IAAuB;IAChD,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,CAAA;IAC9C,MAAM,YAAY,GAAG,IAAI,CAAC,WAAW,EAAE,MAAM,IAAI,EAAE,CAAA;IACnD,MAAM,gBAAgB,GAAG,IAAI,CAAC,qBAAqB,EAAE,MAAM,IAAI,EAAE,CAAA;IACjE,OAAO;QACL,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,CAAC;QACrC,QAAQ,EAAE,SAAS,CAAC,UAAU,EAAE,YAAY,CAAC;QAC7C,UAAU,EAAE,SAAS,CAAC,qBAAqB,EAAE,gBAAgB,CAAC;KAC/D,CAAA;AACH,CAAC","sourcesContent":["import {applicationId} from './session/identity.js'\nimport {validateSession} from './session/validate.js'\nimport {allDefaultScopes, apiScopes} from './session/scopes.js'\nimport {\n exchangeAccessForApplicationTokens,\n exchangeCodeForAccessToken,\n exchangeCustomPartnerToken,\n ExchangeScopes,\n refreshAccessToken,\n InvalidGrantError,\n InvalidRequestError,\n} from './session/exchange.js'\n\nimport {authorize} from './session/authorize.js'\nimport {IdentityToken, Session} from './session/schema.js'\nimport * as secureStore from './session/store.js'\nimport {pollForDeviceAuthorization, requestDeviceAuthorization} from './session/device-authorization.js'\nimport {RequestClientError} from './api/headers.js'\nimport {content, token, debug} from '../../output.js'\nimport {keypress} from '../../ui.js'\n\nimport constants from '../../constants.js'\nimport * as output from '../../output.js'\nimport {firstPartyDev, useDeviceAuth} from '../../public/node/environment/local.js'\nimport {AbortError} from '../../public/node/error.js'\nimport {partnersRequest} from '../../public/node/api/partners.js'\nimport {normalizeStoreFqdn, partnersFqdn, identityFqdn} from '../../public/node/environment/fqdn.js'\nimport {openURL} from '../../public/node/system.js'\nimport {Abort, Bug} from '../../error.js'\nimport {gql} from 'graphql-request'\n\nimport {AdminSession} from '@shopify/cli-kit/node/session'\n\n/**\n * A scope supported by the Shopify Admin API.\n */\ntype AdminAPIScope = 'graphql' | 'themes' | 'collaborator' | string\n\n/**\n * It represents the options to authenticate against the Shopify Admin API.\n */\n\ninterface AdminAPIOAuthOptions {\n /** Store to request permissions for. */\n storeFqdn: string\n /** List of scopes to request permissions for. */\n scopes: AdminAPIScope[]\n}\n\n/**\n * A scope supported by the Partners API.\n */\ntype PartnersAPIScope = 'cli' | string\ninterface PartnersAPIOAuthOptions {\n /** List of scopes to request permissions for. */\n scopes: PartnersAPIScope[]\n}\n\n/**\n * A scope supported by the Storefront Renderer API.\n */\ntype StorefrontRendererScope = 'devtools' | string\ninterface StorefrontRendererAPIOAuthOptions {\n /** List of scopes to request permissions for. */\n scopes: StorefrontRendererScope[]\n}\n\n/**\n * It represents the authentication requirements and\n * is the input necessary to trigger the authentication\n * flow.\n */\nexport interface OAuthApplications {\n adminApi?: AdminAPIOAuthOptions\n storefrontRendererApi?: StorefrontRendererAPIOAuthOptions\n partnersApi?: PartnersAPIOAuthOptions\n}\n\nexport interface OAuthSession {\n admin?: AdminSession\n partners?: string\n storefront?: string\n}\n\n/**\n * This method ensures that we have a valid session to authenticate against the given applications using the provided scopes.\n *\n * @param applications - An object containing the applications we need to be authenticated with.\n * @param env - Optional environment variables to use.\n * @param forceRefresh - Optional flag to force a refresh of the token.\n * @returns An instance with the access tokens organized by application.\n */\nexport async function ensureAuthenticated(\n applications: OAuthApplications,\n env = process.env,\n forceRefresh = false,\n): Promise<OAuthSession> {\n const fqdn = await identityFqdn()\n\n const previousStoreFqdn = applications.adminApi?.storeFqdn\n if (previousStoreFqdn) {\n const normalizedStoreName = await normalizeStoreFqdn(previousStoreFqdn)\n if (previousStoreFqdn === applications.adminApi?.storeFqdn) {\n applications.adminApi.storeFqdn = normalizedStoreName\n }\n }\n\n const currentSession = (await secureStore.fetch()) || {}\n const fqdnSession = currentSession[fqdn]!\n const scopes = getFlattenScopes(applications)\n\n debug(content`Validating existing session against the scopes:\n${token.json(scopes)}\nFor applications:\n${token.json(applications)}\n`)\n const validationResult = await validateSession(scopes, applications, fqdnSession)\n\n let newSession = {}\n\n if (validationResult === 'needs_full_auth') {\n debug(content`Initiating the full authentication flow...`)\n newSession = await executeCompleteFlow(applications, fqdn)\n } else if (validationResult === 'needs_refresh' || forceRefresh) {\n debug(content`The current session is valid but needs refresh. Refreshing...`)\n try {\n newSession = await refreshTokens(fqdnSession.identity, applications, fqdn)\n } catch (error) {\n if (error instanceof InvalidGrantError) {\n newSession = await executeCompleteFlow(applications, fqdn)\n } else if (error instanceof InvalidRequestError) {\n await secureStore.remove()\n throw new AbortError('\\nError validating auth session', \"We've cleared the current session, please try again\")\n } else {\n throw error\n }\n }\n }\n\n const completeSession: Session = {...currentSession, ...newSession}\n await secureStore.store(completeSession)\n const tokens = await tokensFor(applications, completeSession, fqdn)\n\n // Overwrite partners token if using a custom CLI Token\n const envToken = env[constants.environmentVariables.partnersToken]\n if (envToken && applications.partnersApi) {\n tokens.partners = (await exchangeCustomPartnerToken(envToken)).accessToken\n }\n if (!envToken && tokens.partners) {\n await ensureUserHasPartnerAccount(tokens.partners)\n }\n\n return tokens\n}\n\n/**\n * Execute the full authentication flow.\n *\n * @param applications - An object containing the applications we need to be authenticated with.\n * @param identityFqdn - The identity FQDN.\n */\nasync function executeCompleteFlow(applications: OAuthApplications, identityFqdn: string): Promise<Session> {\n const scopes = getFlattenScopes(applications)\n const exchangeScopes = getExchangeScopes(applications)\n const store = applications.adminApi?.storeFqdn\n if (firstPartyDev()) {\n debug(content`Authenticating as Shopify Employee...`)\n scopes.push('employee')\n }\n\n let identityToken: IdentityToken\n if (useDeviceAuth()) {\n // Request a device code to authorize without a browser redirect.\n debug(content`Requesting device authorization code...`)\n const deviceAuth = await requestDeviceAuthorization(scopes)\n\n // Poll for the identity token\n debug(content`Starting polling for the identity token...`)\n identityToken = await pollForDeviceAuthorization(deviceAuth.deviceCode, deviceAuth.interval)\n } else {\n // Authorize user via browser\n debug(content`Authorizing through Identity's website...`)\n const code = await authorize(scopes)\n\n // Exchange code for identity token\n debug(content`Authorization code received. Exchanging it for a CLI token...`)\n identityToken = await exchangeCodeForAccessToken(code)\n }\n\n // Exchange identity token for application tokens\n debug(content`CLI token received. Exchanging it for application tokens...`)\n const result = await exchangeAccessForApplicationTokens(identityToken, exchangeScopes, store)\n\n const session: Session = {\n [identityFqdn]: {\n identity: identityToken,\n applications: result,\n },\n }\n\n output.completed('Logged in.')\n\n return session\n}\n\n/**\n * If the user creates an account from the Identity website, the created\n * account won't get a Partner organization created. We need to detect that\n * and take the user to create a partner organization.\n *\n * @param partnersToken - Partners token.\n */\nasync function ensureUserHasPartnerAccount(partnersToken: string) {\n debug(content`Verifying that the user has a Partner organization`)\n if (!(await hasPartnerAccount(partnersToken))) {\n output.info(`\\nA Shopify Partners organization is needed to proceed.`)\n output.info(`👉 Press any key to create one`)\n await keypress()\n await openURL(`https://${await partnersFqdn()}/signup`)\n output.info(output.content`👉 Press any key when you have ${output.token.cyan('created the organization')}`)\n output.warn(output.content`Make sure you've confirmed your Shopify and the Partner organization from the email`)\n await keypress()\n if (!(await hasPartnerAccount(partnersToken))) {\n throw new Abort(\n `Couldn't find your Shopify Partners organization`,\n `Have you confirmed your accounts from the emails you received?`,\n )\n }\n }\n}\n\n/**\n * Validate if the current token is valid for partners API.\n *\n * @param partnersToken - Partners token.\n * @returns A promise that resolves to true if the token is valid for partners API.\n */\nasync function hasPartnerAccount(partnersToken: string): Promise<boolean> {\n try {\n await partnersRequest(\n gql`\n {\n organizations(first: 1) {\n nodes {\n id\n }\n }\n }\n `,\n partnersToken,\n )\n return true\n // eslint-disable-next-line no-catch-all/no-catch-all\n } catch (error) {\n if (error instanceof RequestClientError && error.statusCode === 404) {\n return false\n } else {\n return true\n }\n }\n}\n\n/**\n * Refresh the tokens for a given session.\n *\n * @param token - Identity token.\n * @param applications - An object containing the applications we need to be authenticated with.\n * @param fqdn - The identity FQDN.\n */\nasync function refreshTokens(token: IdentityToken, applications: OAuthApplications, fqdn: string): Promise<Session> {\n // Refresh Identity Token\n const identityToken = await refreshAccessToken(token)\n // Exchange new identity token for application tokens\n const exchangeScopes = getExchangeScopes(applications)\n const applicationTokens = await exchangeAccessForApplicationTokens(\n identityToken,\n exchangeScopes,\n applications.adminApi?.storeFqdn,\n )\n\n return {\n [fqdn]: {\n identity: identityToken,\n applications: applicationTokens,\n },\n }\n}\n\n/**\n * Get the application tokens for a given session.\n *\n * @param applications - An object containing the applications we need the tokens for.\n * @param session - The current session.\n * @param fqdn - The identity FQDN.\n */\nasync function tokensFor(applications: OAuthApplications, session: Session, fqdn: string): Promise<OAuthSession> {\n const fqdnSession = session[fqdn]\n if (!fqdnSession) {\n throw new Bug('No session found after ensuring authenticated')\n }\n const tokens: OAuthSession = {}\n if (applications.adminApi) {\n const appId = applicationId('admin')\n const realAppId = `${applications.adminApi.storeFqdn}-${appId}`\n const token = fqdnSession.applications[realAppId]?.accessToken\n if (token) {\n tokens.admin = {token, storeFqdn: applications.adminApi.storeFqdn}\n }\n }\n\n if (applications.partnersApi) {\n const appId = applicationId('partners')\n tokens.partners = fqdnSession.applications[appId]?.accessToken\n }\n\n if (applications.storefrontRendererApi) {\n const appId = applicationId('storefront-renderer')\n tokens.storefront = fqdnSession.applications[appId]?.accessToken\n }\n return tokens\n}\n\n// Scope Helpers\n/**\n * Get a flattened array of scopes for the given applications.\n *\n * @param apps - An object containing the applications we need the scopes for.\n * @returns A flattened array of scopes.\n */\nfunction getFlattenScopes(apps: OAuthApplications): string[] {\n const admin = apps.adminApi?.scopes || []\n const partner = apps.partnersApi?.scopes || []\n const storefront = apps.storefrontRendererApi?.scopes || []\n const requestedScopes = [...admin, ...partner, ...storefront]\n return allDefaultScopes(requestedScopes)\n}\n\n/**\n * Get the scopes for the given applications.\n *\n * @param apps - An object containing the applications we need the scopes for.\n * @returns An object containing the scopes for each application.\n */\nfunction getExchangeScopes(apps: OAuthApplications): ExchangeScopes {\n const adminScope = apps.adminApi?.scopes || []\n const partnerScope = apps.partnersApi?.scopes || []\n const storefrontScopes = apps.storefrontRendererApi?.scopes || []\n return {\n admin: apiScopes('admin', adminScope),\n partners: apiScopes('partners', partnerScope),\n storefront: apiScopes('storefront-renderer', storefrontScopes),\n }\n}\n"]}
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../../src/private/node/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,uBAAuB,CAAA;AACnD,OAAO,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAA;AACrD,OAAO,EAAC,gBAAgB,EAAE,SAAS,EAAC,MAAM,qBAAqB,CAAA;AAC/D,OAAO,EACL,kCAAkC,EAClC,0BAA0B,EAC1B,0BAA0B,EAE1B,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAC,SAAS,EAAC,MAAM,wBAAwB,CAAA;AAEhD,OAAO,KAAK,WAAW,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAC,0BAA0B,EAAE,0BAA0B,EAAC,MAAM,mCAAmC,CAAA;AACxG,OAAO,EAAC,kBAAkB,EAAC,MAAM,kBAAkB,CAAA;AACnD,OAAO,EAAC,oBAAoB,EAAC,MAAM,gBAAgB,CAAA;AACnD,OAA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,WAAW,EAAE,MAAM,IAAI,EAAE,CAAA;IACnD,MAAM,gBAAgB,GAAG,IAAI,CAAC,qBAAqB,EAAE,MAAM,IAAI,EAAE,CAAA;IACjE,OAAO;QACL,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,UAAU,CAAC;QACrC,QAAQ,EAAE,SAAS,CAAC,UAAU,EAAE,YAAY,CAAC;QAC7C,UAAU,EAAE,SAAS,CAAC,qBAAqB,EAAE,gBAAgB,CAAC;KAC/D,CAAA;AACH,CAAC","sourcesContent":["import {applicationId} from './session/identity.js'\nimport {validateSession} from './session/validate.js'\nimport {allDefaultScopes, apiScopes} from './session/scopes.js'\nimport {\n exchangeAccessForApplicationTokens,\n exchangeCodeForAccessToken,\n exchangeCustomPartnerToken,\n ExchangeScopes,\n refreshAccessToken,\n InvalidGrantError,\n InvalidRequestError,\n} from './session/exchange.js'\nimport {authorize} from './session/authorize.js'\nimport {IdentityToken, Session} from './session/schema.js'\nimport * as secureStore from './session/store.js'\nimport {pollForDeviceAuthorization, requestDeviceAuthorization} from './session/device-authorization.js'\nimport {RequestClientError} from './api/headers.js'\nimport {environmentVariables} from './constants.js'\nimport {content, token, debug} from '../../output.js'\nimport {keypress} from '../../ui.js'\nimport * as output from '../../output.js'\nimport {firstPartyDev, useDeviceAuth} from '../../public/node/environment/local.js'\nimport {AbortError} from '../../public/node/error.js'\nimport {partnersRequest} from '../../public/node/api/partners.js'\nimport {normalizeStoreFqdn, partnersFqdn, identityFqdn} from '../../public/node/environment/fqdn.js'\nimport {openURL} from '../../public/node/system.js'\nimport {Abort, Bug} from '../../error.js'\nimport {gql} from 'graphql-request'\nimport {AdminSession} from '@shopify/cli-kit/node/session'\n\n/**\n * A scope supported by the Shopify Admin API.\n */\ntype AdminAPIScope = 'graphql' | 'themes' | 'collaborator' | string\n\n/**\n * It represents the options to authenticate against the Shopify Admin API.\n */\n\ninterface AdminAPIOAuthOptions {\n /** Store to request permissions for. */\n storeFqdn: string\n /** List of scopes to request permissions for. */\n scopes: AdminAPIScope[]\n}\n\n/**\n * A scope supported by the Partners API.\n */\ntype PartnersAPIScope = 'cli' | string\ninterface PartnersAPIOAuthOptions {\n /** List of scopes to request permissions for. */\n scopes: PartnersAPIScope[]\n}\n\n/**\n * A scope supported by the Storefront Renderer API.\n */\ntype StorefrontRendererScope = 'devtools' | string\ninterface StorefrontRendererAPIOAuthOptions {\n /** List of scopes to request permissions for. */\n scopes: StorefrontRendererScope[]\n}\n\n/**\n * It represents the authentication requirements and\n * is the input necessary to trigger the authentication\n * flow.\n */\nexport interface OAuthApplications {\n adminApi?: AdminAPIOAuthOptions\n storefrontRendererApi?: StorefrontRendererAPIOAuthOptions\n partnersApi?: PartnersAPIOAuthOptions\n}\n\nexport interface OAuthSession {\n admin?: AdminSession\n partners?: string\n storefront?: string\n}\n\n/**\n * This method ensures that we have a valid session to authenticate against the given applications using the provided scopes.\n *\n * @param applications - An object containing the applications we need to be authenticated with.\n * @param env - Optional environment variables to use.\n * @param forceRefresh - Optional flag to force a refresh of the token.\n * @returns An instance with the access tokens organized by application.\n */\nexport async function ensureAuthenticated(\n applications: OAuthApplications,\n env = process.env,\n forceRefresh = false,\n): Promise<OAuthSession> {\n const fqdn = await identityFqdn()\n\n const previousStoreFqdn = applications.adminApi?.storeFqdn\n if (previousStoreFqdn) {\n const normalizedStoreName = await normalizeStoreFqdn(previousStoreFqdn)\n if (previousStoreFqdn === applications.adminApi?.storeFqdn) {\n applications.adminApi.storeFqdn = normalizedStoreName\n }\n }\n\n const currentSession = (await secureStore.fetch()) || {}\n const fqdnSession = currentSession[fqdn]!\n const scopes = getFlattenScopes(applications)\n\n debug(content`Validating existing session against the scopes:\n${token.json(scopes)}\nFor applications:\n${token.json(applications)}\n`)\n const validationResult = await validateSession(scopes, applications, fqdnSession)\n\n let newSession = {}\n\n if (validationResult === 'needs_full_auth') {\n debug(content`Initiating the full authentication flow...`)\n newSession = await executeCompleteFlow(applications, fqdn)\n } else if (validationResult === 'needs_refresh' || forceRefresh) {\n debug(content`The current session is valid but needs refresh. Refreshing...`)\n try {\n newSession = await refreshTokens(fqdnSession.identity, applications, fqdn)\n } catch (error) {\n if (error instanceof InvalidGrantError) {\n newSession = await executeCompleteFlow(applications, fqdn)\n } else if (error instanceof InvalidRequestError) {\n await secureStore.remove()\n throw new AbortError('\\nError validating auth session', \"We've cleared the current session, please try again\")\n } else {\n throw error\n }\n }\n }\n\n const completeSession: Session = {...currentSession, ...newSession}\n await secureStore.store(completeSession)\n const tokens = await tokensFor(applications, completeSession, fqdn)\n\n // Overwrite partners token if using a custom CLI Token\n const envToken = env[environmentVariables.partnersToken]\n if (envToken && applications.partnersApi) {\n tokens.partners = (await exchangeCustomPartnerToken(envToken)).accessToken\n }\n if (!envToken && tokens.partners) {\n await ensureUserHasPartnerAccount(tokens.partners)\n }\n\n return tokens\n}\n\n/**\n * Execute the full authentication flow.\n *\n * @param applications - An object containing the applications we need to be authenticated with.\n * @param identityFqdn - The identity FQDN.\n */\nasync function executeCompleteFlow(applications: OAuthApplications, identityFqdn: string): Promise<Session> {\n const scopes = getFlattenScopes(applications)\n const exchangeScopes = getExchangeScopes(applications)\n const store = applications.adminApi?.storeFqdn\n if (firstPartyDev()) {\n debug(content`Authenticating as Shopify Employee...`)\n scopes.push('employee')\n }\n\n let identityToken: IdentityToken\n if (useDeviceAuth()) {\n // Request a device code to authorize without a browser redirect.\n debug(content`Requesting device authorization code...`)\n const deviceAuth = await requestDeviceAuthorization(scopes)\n\n // Poll for the identity token\n debug(content`Starting polling for the identity token...`)\n identityToken = await pollForDeviceAuthorization(deviceAuth.deviceCode, deviceAuth.interval)\n } else {\n // Authorize user via browser\n debug(content`Authorizing through Identity's website...`)\n const code = await authorize(scopes)\n\n // Exchange code for identity token\n debug(content`Authorization code received. Exchanging it for a CLI token...`)\n identityToken = await exchangeCodeForAccessToken(code)\n }\n\n // Exchange identity token for application tokens\n debug(content`CLI token received. Exchanging it for application tokens...`)\n const result = await exchangeAccessForApplicationTokens(identityToken, exchangeScopes, store)\n\n const session: Session = {\n [identityFqdn]: {\n identity: identityToken,\n applications: result,\n },\n }\n\n output.completed('Logged in.')\n\n return session\n}\n\n/**\n * If the user creates an account from the Identity website, the created\n * account won't get a Partner organization created. We need to detect that\n * and take the user to create a partner organization.\n *\n * @param partnersToken - Partners token.\n */\nasync function ensureUserHasPartnerAccount(partnersToken: string) {\n debug(content`Verifying that the user has a Partner organization`)\n if (!(await hasPartnerAccount(partnersToken))) {\n output.info(`\\nA Shopify Partners organization is needed to proceed.`)\n output.info(`👉 Press any key to create one`)\n await keypress()\n await openURL(`https://${await partnersFqdn()}/signup`)\n output.info(output.content`👉 Press any key when you have ${output.token.cyan('created the organization')}`)\n output.warn(output.content`Make sure you've confirmed your Shopify and the Partner organization from the email`)\n await keypress()\n if (!(await hasPartnerAccount(partnersToken))) {\n throw new Abort(\n `Couldn't find your Shopify Partners organization`,\n `Have you confirmed your accounts from the emails you received?`,\n )\n }\n }\n}\n\n/**\n * Validate if the current token is valid for partners API.\n *\n * @param partnersToken - Partners token.\n * @returns A promise that resolves to true if the token is valid for partners API.\n */\nasync function hasPartnerAccount(partnersToken: string): Promise<boolean> {\n try {\n await partnersRequest(\n gql`\n {\n organizations(first: 1) {\n nodes {\n id\n }\n }\n }\n `,\n partnersToken,\n )\n return true\n // eslint-disable-next-line no-catch-all/no-catch-all\n } catch (error) {\n if (error instanceof RequestClientError && error.statusCode === 404) {\n return false\n } else {\n return true\n }\n }\n}\n\n/**\n * Refresh the tokens for a given session.\n *\n * @param token - Identity token.\n * @param applications - An object containing the applications we need to be authenticated with.\n * @param fqdn - The identity FQDN.\n */\nasync function refreshTokens(token: IdentityToken, applications: OAuthApplications, fqdn: string): Promise<Session> {\n // Refresh Identity Token\n const identityToken = await refreshAccessToken(token)\n // Exchange new identity token for application tokens\n const exchangeScopes = getExchangeScopes(applications)\n const applicationTokens = await exchangeAccessForApplicationTokens(\n identityToken,\n exchangeScopes,\n applications.adminApi?.storeFqdn,\n )\n\n return {\n [fqdn]: {\n identity: identityToken,\n applications: applicationTokens,\n },\n }\n}\n\n/**\n * Get the application tokens for a given session.\n *\n * @param applications - An object containing the applications we need the tokens for.\n * @param session - The current session.\n * @param fqdn - The identity FQDN.\n */\nasync function tokensFor(applications: OAuthApplications, session: Session, fqdn: string): Promise<OAuthSession> {\n const fqdnSession = session[fqdn]\n if (!fqdnSession) {\n throw new Bug('No session found after ensuring authenticated')\n }\n const tokens: OAuthSession = {}\n if (applications.adminApi) {\n const appId = applicationId('admin')\n const realAppId = `${applications.adminApi.storeFqdn}-${appId}`\n const token = fqdnSession.applications[realAppId]?.accessToken\n if (token) {\n tokens.admin = {token, storeFqdn: applications.adminApi.storeFqdn}\n }\n }\n\n if (applications.partnersApi) {\n const appId = applicationId('partners')\n tokens.partners = fqdnSession.applications[appId]?.accessToken\n }\n\n if (applications.storefrontRendererApi) {\n const appId = applicationId('storefront-renderer')\n tokens.storefront = fqdnSession.applications[appId]?.accessToken\n }\n return tokens\n}\n\n// Scope Helpers\n/**\n * Get a flattened array of scopes for the given applications.\n *\n * @param apps - An object containing the applications we need the scopes for.\n * @returns A flattened array of scopes.\n */\nfunction getFlattenScopes(apps: OAuthApplications): string[] {\n const admin = apps.adminApi?.scopes || []\n const partner = apps.partnersApi?.scopes || []\n const storefront = apps.storefrontRendererApi?.scopes || []\n const requestedScopes = [...admin, ...partner, ...storefront]\n return allDefaultScopes(requestedScopes)\n}\n\n/**\n * Get the scopes for the given applications.\n *\n * @param apps - An object containing the applications we need the scopes for.\n * @returns An object containing the scopes for each application.\n */\nfunction getExchangeScopes(apps: OAuthApplications): ExchangeScopes {\n const adminScope = apps.adminApi?.scopes || []\n const partnerScope = apps.partnersApi?.scopes || []\n const storefrontScopes = apps.storefrontRendererApi?.scopes || []\n return {\n admin: apiScopes('admin', adminScope),\n partners: apiScopes('partners', partnerScope),\n storefront: apiScopes('storefront-renderer', storefrontScopes),\n }\n}\n"]}
@@ -1,8 +1,9 @@
1
1
  import { Props as SelectProps } from './SelectInput.js';
2
2
  import { Props as TableProps } from './Table.js';
3
+ import { TokenItem } from './TokenizedText.js';
3
4
  import React, { ReactElement } from 'react';
4
5
  export interface Props<T> {
5
- message: string;
6
+ message: TokenItem;
6
7
  choices: SelectProps<T>['items'];
7
8
  onSubmit: (value: T) => void;
8
9
  infoTable?: TableProps['table'];
@@ -1,5 +1,6 @@
1
1
  import SelectInput from './SelectInput.js';
2
2
  import Table from './Table.js';
3
+ import { TokenizedText } from './TokenizedText.js';
3
4
  import { handleCtrlC } from '../../ui.js';
4
5
  import React, { useCallback, useState } from 'react';
5
6
  import { Box, measureElement, Text, useApp, useInput, useStdout } from 'ink';
@@ -32,7 +33,7 @@ function SelectPrompt({ message, choices, infoTable, onSubmit, }) {
32
33
  React.createElement(Box, null,
33
34
  React.createElement(Box, { marginRight: 2 },
34
35
  React.createElement(Text, null, "?")),
35
- React.createElement(Text, null, message)),
36
+ React.createElement(TokenizedText, { item: message })),
36
37
  infoTable && !submitted && (React.createElement(Box, { marginLeft: 7 },
37
38
  React.createElement(Table, { table: infoTable }))),
38
39
  submitted ? (React.createElement(Box, null,
@@ -1 +1 @@
1
- {"version":3,"file":"SelectPrompt.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/SelectPrompt.tsx"],"names":[],"mappings":"AAAA,OAAO,WAA6D,MAAM,kBAAkB,CAAA;AAC5F,OAAO,KAA4B,MAAM,YAAY,CAAA;AACrD,OAAO,EAAC,WAAW,EAAC,MAAM,aAAa,CAAA;AACvC,OAAO,KAAK,EAAE,EAAe,WAAW,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAA;AAChE,OAAO,EAAC,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAC,MAAM,KAAK,CAAA;AAC1E,OAAO,EAAC,OAAO,EAAC,MAAM,QAAQ,CAAA;AAC9B,OAAO,WAAW,MAAM,cAAc,CAAA;AAStC,SAAS,YAAY,CAAI,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AE,WAAW;QAC3D,oBAAC,GAAG;YACF,oBAAC,GAAG,IAAC,WAAW,EAAE,CAAC;gBACjB,oBAAC,IAAI,YAAS,CACV;YACN,oBAAC,IAAI,QAAE,OAAO,CAAQ,CAClB;QACL,SAAS,IAAI,CAAC,SAAS,IAAI,CAC1B,oBAAC,GAAG,IAAC,UAAU,EAAE,CAAC;YAChB,oBAAC,KAAK,IAAC,KAAK,EAAE,SAAS,GAAI,CACvB,CACP;QACA,SAAS,CAAC,CAAC,CAAC,CACX,oBAAC,GAAG;YACF,oBAAC,GAAG,IAAC,WAAW,EAAE,CAAC;gBACjB,oBAAC,IAAI,IAAC,KAAK,EAAC,MAAM,IAAE,OAAO,CAAC,IAAI,CAAQ,CACpC;YAEN,oBAAC,IAAI,IAAC,KAAK,EAAC,MAAM,IAAE,MAAM,CAAC,KAAK,CAAQ,CACpC,CACP,CAAC,CAAC,CAAC,CACF,oBAAC,WAAW,IACV,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,CAAC,IAAa,EAAE,EAAE;gBAC1B,SAAS,CAAC,IAAI,CAAC,CAAA;YACjB,CAAC,GACD,CACH,CACG,CACP,CAAA;AACH,CAAC;AAED,OAAO,EAAC,YAAY,EAAC,CAAA","sourcesContent":["import SelectInput, {Props as SelectProps, Item as SelectItem, Item} from './SelectInput.js'\nimport Table, {Props as TableProps} from './Table.js'\nimport {handleCtrlC} from '../../ui.js'\nimport React, {ReactElement, useCallback, useState} from 'react'\nimport {Box, measureElement, Text, useApp, useInput, useStdout} from 'ink'\nimport {figures} from 'listr2'\nimport ansiEscapes from 'ansi-escapes'\n\nexport interface Props<T> {\n message: string\n choices: SelectProps<T>['items']\n onSubmit: (value: T) => void\n infoTable?: TableProps['table']\n}\n\nfunction SelectPrompt<T>({\n message,\n choices,\n infoTable,\n onSubmit,\n}: React.PropsWithChildren<Props<T>>): ReactElement | null {\n const [answer, setAnswer] = useState<SelectItem<T>>(choices[0]!)\n const {exit: unmountInk} = useApp()\n const [submitted, setSubmitted] = useState(false)\n const {stdout} = useStdout()\n const [height, setHeight] = useState(0)\n\n const measuredRef = useCallback((node) => {\n if (node !== null) {\n const {height} = measureElement(node)\n setHeight(height)\n }\n }, [])\n\n useInput(\n useCallback(\n (input, key) => {\n handleCtrlC(input, key)\n\n if (key.return) {\n if (stdout && height >= stdout.rows) {\n stdout.write(ansiEscapes.clearTerminal)\n }\n setSubmitted(true)\n unmountInk()\n onSubmit(answer.value)\n }\n },\n [answer, onSubmit, height],\n ),\n )\n\n return (\n <Box flexDirection=\"column\" marginBottom={1} ref={measuredRef}>\n <Box>\n <Box marginRight={2}>\n <Text>?</Text>\n </Box>\n <Text>{message}</Text>\n </Box>\n {infoTable && !submitted && (\n <Box marginLeft={7}>\n <Table table={infoTable} />\n </Box>\n )}\n {submitted ? (\n <Box>\n <Box marginRight={2}>\n <Text color=\"cyan\">{figures.tick}</Text>\n </Box>\n\n <Text color=\"cyan\">{answer.label}</Text>\n </Box>\n ) : (\n <SelectInput\n items={choices}\n onChange={(item: Item<T>) => {\n setAnswer(item)\n }}\n />\n )}\n </Box>\n )\n}\n\nexport {SelectPrompt}\n"]}
1
+ {"version":3,"file":"SelectPrompt.js","sourceRoot":"","sources":["../../../../../src/private/node/ui/components/SelectPrompt.tsx"],"names":[],"mappings":"AAAA,OAAO,WAA6D,MAAM,kBAAkB,CAAA;AAC5F,OAAO,KAA4B,MAAM,YAAY,CAAA;AACrD,OAAO,EAAY,aAAa,EAAC,MAAM,oBAAoB,CAAA;AAC3D,OAAO,EAAC,WAAW,EAAC,MAAM,aAAa,CAAA;AACvC,OAAO,KAAK,EAAE,EAAe,WAAW,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAA;AAChE,OAAO,EAAC,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAC,MAAM,KAAK,CAAA;AAC1E,OAAO,EAAC,OAAO,EAAC,MAAM,QAAQ,CAAA;AAC9B,OAAO,WAAW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AQ,EAAC,YAAY,EAAE,CAAC,EAAE,GAAG,EAAE,WAAW;QAC3D,oBAAC,GAAG;YACF,oBAAC,GAAG,IAAC,WAAW,EAAE,CAAC;gBACjB,oBAAC,IAAI,YAAS,CACV;YACN,oBAAC,aAAa,IAAC,IAAI,EAAE,OAAO,GAAI,CAC5B;QACL,SAAS,IAAI,CAAC,SAAS,IAAI,CAC1B,oBAAC,GAAG,IAAC,UAAU,EAAE,CAAC;YAChB,oBAAC,KAAK,IAAC,KAAK,EAAE,SAAS,GAAI,CACvB,CACP;QACA,SAAS,CAAC,CAAC,CAAC,CACX,oBAAC,GAAG;YACF,oBAAC,GAAG,IAAC,WAAW,EAAE,CAAC;gBACjB,oBAAC,IAAI,IAAC,KAAK,EAAC,MAAM,IAAE,OAAO,CAAC,IAAI,CAAQ,CACpC;YAEN,oBAAC,IAAI,IAAC,KAAK,EAAC,MAAM,IAAE,MAAM,CAAC,KAAK,CAAQ,CACpC,CACP,CAAC,CAAC,CAAC,CACF,oBAAC,WAAW,IACV,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,CAAC,IAAa,EAAE,EAAE;gBAC1B,SAAS,CAAC,IAAI,CAAC,CAAA;YACjB,CAAC,GACD,CACH,CACG,CACP,CAAA;AACH,CAAC;AAED,OAAO,EAAC,YAAY,EAAC,CAAA","sourcesContent":["import SelectInput, {Props as SelectProps, Item as SelectItem, Item} from './SelectInput.js'\nimport Table, {Props as TableProps} from './Table.js'\nimport {TokenItem, TokenizedText} from './TokenizedText.js'\nimport {handleCtrlC} from '../../ui.js'\nimport React, {ReactElement, useCallback, useState} from 'react'\nimport {Box, measureElement, Text, useApp, useInput, useStdout} from 'ink'\nimport {figures} from 'listr2'\nimport ansiEscapes from 'ansi-escapes'\n\nexport interface Props<T> {\n message: TokenItem\n choices: SelectProps<T>['items']\n onSubmit: (value: T) => void\n infoTable?: TableProps['table']\n}\n\nfunction SelectPrompt<T>({\n message,\n choices,\n infoTable,\n onSubmit,\n}: React.PropsWithChildren<Props<T>>): ReactElement | null {\n const [answer, setAnswer] = useState<SelectItem<T>>(choices[0]!)\n const {exit: unmountInk} = useApp()\n const [submitted, setSubmitted] = useState(false)\n const {stdout} = useStdout()\n const [height, setHeight] = useState(0)\n\n const measuredRef = useCallback((node) => {\n if (node !== null) {\n const {height} = measureElement(node)\n setHeight(height)\n }\n }, [])\n\n useInput(\n useCallback(\n (input, key) => {\n handleCtrlC(input, key)\n\n if (key.return) {\n if (stdout && height >= stdout.rows) {\n stdout.write(ansiEscapes.clearTerminal)\n }\n setSubmitted(true)\n unmountInk()\n onSubmit(answer.value)\n }\n },\n [answer, onSubmit, height],\n ),\n )\n\n return (\n <Box flexDirection=\"column\" marginBottom={1} ref={measuredRef}>\n <Box>\n <Box marginRight={2}>\n <Text>?</Text>\n </Box>\n <TokenizedText item={message} />\n </Box>\n {infoTable && !submitted && (\n <Box marginLeft={7}>\n <Table table={infoTable} />\n </Box>\n )}\n {submitted ? (\n <Box>\n <Box marginRight={2}>\n <Text color=\"cyan\">{figures.tick}</Text>\n </Box>\n\n <Text color=\"cyan\">{answer.label}</Text>\n </Box>\n ) : (\n <SelectInput\n items={choices}\n onChange={(item: Item<T>) => {\n setAnswer(item)\n }}\n />\n )}\n </Box>\n )\n}\n\nexport {SelectPrompt}\n"]}
@@ -0,0 +1,5 @@
1
+ export interface Column<T> {
2
+ name: keyof T;
3
+ width: number;
4
+ color?: string;
5
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Column.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Column.js","sourceRoot":"","sources":["../../../../../../src/private/node/ui/components/Table/Column.ts"],"names":[],"mappings":"","sourcesContent":["export interface Column<T> {\n name: keyof T\n width: number\n color?: string\n}\n"]}
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ import ScalarDict from './ScalarDict.js';
3
+ import { Column } from './Column.js';
4
+ interface RowProps<T extends ScalarDict> {
5
+ fillerChar: string;
6
+ rowKey: string;
7
+ data: Partial<T>;
8
+ columns: Column<T>[];
9
+ ignoreColumnColor?: boolean;
10
+ }
11
+ export declare function Row<T extends ScalarDict>({ rowKey, columns, data, fillerChar, ignoreColumnColor }: RowProps<T>): JSX.Element;
12
+ export {};
@@ -0,0 +1,24 @@
1
+ import { Box, Text } from 'ink';
2
+ import React from 'react';
3
+ function join(elements, separator) {
4
+ return elements.reduce((elements, element, index) => {
5
+ if (elements.length === 0) {
6
+ return [element];
7
+ }
8
+ return [...elements, separator(index), element];
9
+ }, []);
10
+ }
11
+ export function Row({ rowKey, columns, data, fillerChar, ignoreColumnColor }) {
12
+ return (React.createElement(Box, { flexDirection: "row" }, ...join(columns.map((column) => {
13
+ const content = data[column.name];
14
+ const key = `${rowKey}-cell-${column.name.toString()}`;
15
+ const marginRight = column.width - String(content ?? '').length;
16
+ return (React.createElement(Text, { key: key, color: ignoreColumnColor ? undefined : column.color },
17
+ content,
18
+ fillerChar.repeat(marginRight)));
19
+ }), (index) => {
20
+ const key = `${rowKey}-horizontal-separator-${index}`;
21
+ return React.createElement(Text, { key: key }, ' ');
22
+ })));
23
+ }
24
+ //# sourceMappingURL=Row.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Row.js","sourceRoot":"","sources":["../../../../../../src/private/node/ui/components/Table/Row.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAC,GAAG,EAAE,IAAI,EAAC,MAAM,KAAK,CAAA;AAC7B,OAAO,KAAK,MAAM,OAAO,CAAA;AAUzB,SAAS,IAAI,CAAQ,QAAa,EAAE,SAAgC;IAClE,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;QAClD,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;YACzB,OAAO,CAAC,OAAO,CAAC,CAAA;SACjB;QACD,OAAO,CAAC,GAAG,QAAQ,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAA;IACjD,CAAC,EAAE,EAAgB,CAAC,CAAA;AACtB,CAAC;AAED,MAAM,UAAU,GAAG,CAAuB,EAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,iBAAiB,EAAc;IAC3G,OAAO,CACL,oBAAC,GAAG,IAAC,aAAa,EAAC,KAAK,OAClB,IAAI,CACN,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;QACrB,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QACjC,MAAM,GAAG,GAAG,GAAG,MAAM,SAAS,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAA;QACtD,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,MAAM,CAAA;QAE/D,OAAO,CACL,oBAAC,IAAI,IAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK;YAChE,OAAO;YACP,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,CAC1B,CACR,CAAA;IACH,CAAC,CAAC,EAEF,CAAC,KAAK,EAAE,EAAE;QACR,MAAM,GAAG,GAAG,GAAG,MAAM,yBAAyB,KAAK,EAAE,CAAA;QACrD,OAAO,oBAAC,IAAI,IAAC,GAAG,EAAE,GAAG,IAAG,IAAI,CAAQ,CAAA;IACtC,CAAC,CACF,CACG,CACP,CAAA;AACH,CAAC","sourcesContent":["import ScalarDict from './ScalarDict.js'\nimport {Column} from './Column.js'\nimport {Box, Text} from 'ink'\nimport React from 'react'\n\ninterface RowProps<T extends ScalarDict> {\n fillerChar: string\n rowKey: string\n data: Partial<T>\n columns: Column<T>[]\n ignoreColumnColor?: boolean\n}\n\nfunction join<T, TI>(elements: T[], separator: (index: number) => TI): (T | TI)[] {\n return elements.reduce((elements, element, index) => {\n if (elements.length === 0) {\n return [element]\n }\n return [...elements, separator(index), element]\n }, [] as (T | TI)[])\n}\n\nexport function Row<T extends ScalarDict>({rowKey, columns, data, fillerChar, ignoreColumnColor}: RowProps<T>) {\n return (\n <Box flexDirection=\"row\">\n {...join(\n columns.map((column) => {\n const content = data[column.name]\n const key = `${rowKey}-cell-${column.name.toString()}`\n const marginRight = column.width - String(content ?? '').length\n\n return (\n <Text key={key} color={ignoreColumnColor ? undefined : column.color}>\n {content}\n {fillerChar.repeat(marginRight)}\n </Text>\n )\n }),\n\n (index) => {\n const key = `${rowKey}-horizontal-separator-${index}`\n return <Text key={key}>{' '}</Text>\n },\n )}\n </Box>\n )\n}\n"]}
@@ -0,0 +1,5 @@
1
+ declare type Scalar = string | number | boolean | null | undefined;
2
+ export default interface ScalarDict {
3
+ [key: string]: Scalar;
4
+ }
5
+ export {};
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ScalarDict.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ScalarDict.js","sourceRoot":"","sources":["../../../../../../src/private/node/ui/components/Table/ScalarDict.ts"],"names":[],"mappings":"","sourcesContent":["type Scalar = string | number | boolean | null | undefined\n\nexport default interface ScalarDict {\n [key: string]: Scalar\n}\n"]}
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ import ScalarDict from './ScalarDict.js';
3
+ export interface TableProps<T extends ScalarDict> {
4
+ rows: T[];
5
+ columns: {
6
+ [column in keyof T]: {
7
+ header?: string;
8
+ color?: string;
9
+ };
10
+ };
11
+ }
12
+ export default function Table<T extends ScalarDict>({ rows, columns: columnsConfiguration }: TableProps<T>): JSX.Element;
@@ -0,0 +1,30 @@
1
+ import { Row } from './Row.js';
2
+ import React from 'react';
3
+ import { Box } from 'ink';
4
+ export default function Table({ rows, columns: columnsConfiguration }) {
5
+ const columns = Object.entries(columnsConfiguration).map(([key, { header, color }]) => {
6
+ const headerWidth = String(header || key).length;
7
+ const columnWidths = rows.map((row) => {
8
+ const value = row[key];
9
+ if (value === undefined || value === null) {
10
+ return 0;
11
+ }
12
+ return String(value).length;
13
+ });
14
+ return {
15
+ name: key,
16
+ width: Math.max(...columnWidths, headerWidth),
17
+ color,
18
+ };
19
+ });
20
+ const headings = Object.entries(columnsConfiguration).reduce((headings, [column, { header }]) => ({ ...headings, [column]: header || column }), {});
21
+ return (React.createElement(Box, { flexDirection: "column" },
22
+ React.createElement(Row, { rowKey: "heading", fillerChar: " ", columns: columns, data: headings, ignoreColumnColor: true }),
23
+ React.createElement(Row, { rowKey: "separator", fillerChar: "\u2500", columns: columns, data: {}, ignoreColumnColor: true }),
24
+ rows.map((row, index) => {
25
+ const key = `row-${index}`;
26
+ return (React.createElement(Box, { flexDirection: "column", key: key },
27
+ React.createElement(Row, { rowKey: `data-${key}`, fillerChar: " ", columns: columns, data: row })));
28
+ })));
29
+ }
30
+ //# sourceMappingURL=Table.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Table.js","sourceRoot":"","sources":["../../../../../../src/private/node/ui/components/Table/Table.tsx"],"names":[],"mappings":"AACA,OAAO,EAAC,GAAG,EAAC,MAAM,UAAU,CAAA;AAC5B,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAC,GAAG,EAAC,MAAM,KAAK,CAAA;AAOvB,MAAM,CAAC,OAAO,UAAU,KAAK,CAAuB,EAAC,IAAI,EAAE,OAAO,EAAE,oBAAoB,EAAgB;IACtG,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,EAAC,MAAM,EAAE,KAAK,EAAC,CAAC,EAAE,EAAE;QAClF,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAA;QAChD,MAAM,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;YACpC,MAAM,KAAK,GAAG,GAAG,CAAC,GAA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AAO,EAAE,IAAI,EAAE,GAAG,GAAI,CACtE,CACP,CAAA;QACH,CAAC,CAAC,CACE,CACP,CAAA;AACH,CAAC","sourcesContent":["import ScalarDict from './ScalarDict.js'\nimport {Row} from './Row.js'\nimport React from 'react'\nimport {Box} from 'ink'\n\nexport interface TableProps<T extends ScalarDict> {\n rows: T[]\n columns: {[column in keyof T]: {header?: string; color?: string}}\n}\n\nexport default function Table<T extends ScalarDict>({rows, columns: columnsConfiguration}: TableProps<T>) {\n const columns = Object.entries(columnsConfiguration).map(([key, {header, color}]) => {\n const headerWidth = String(header || key).length\n const columnWidths = rows.map((row) => {\n const value = row[key]\n\n if (value === undefined || value === null) {\n return 0\n }\n\n return String(value).length\n })\n\n return {\n name: key,\n width: Math.max(...columnWidths, headerWidth),\n color,\n }\n })\n const headings = Object.entries(columnsConfiguration).reduce(\n (headings, [column, {header}]) => ({...headings, [column]: header || column}),\n {},\n )\n\n return (\n <Box flexDirection=\"column\">\n <Row rowKey=\"heading\" fillerChar=\" \" columns={columns} data={headings} ignoreColumnColor />\n <Row rowKey=\"separator\" fillerChar=\"─\" columns={columns} data={{}} ignoreColumnColor />\n {rows.map((row, index) => {\n const key = `row-${index}`\n\n return (\n <Box flexDirection=\"column\" key={key}>\n <Row rowKey={`data-${key}`} fillerChar=\" \" columns={columns} data={row} />\n </Box>\n )\n })}\n </Box>\n )\n}\n"]}
@@ -0,0 +1,41 @@
1
+ import Table from './Table.js';
2
+ import { describe, expect, test } from 'vitest';
3
+ import React from 'react';
4
+ import { render } from 'ink-testing-library';
5
+ describe('Table', async () => {
6
+ test('formats the table correctly', async () => {
7
+ const rows = [
8
+ { id: '#1361', name: 'Dawn', role: '[live]' },
9
+ { id: '#1363', name: 'Studio', role: '' },
10
+ { id: '#1374', name: 'Debut', role: '[unpublished]' },
11
+ {
12
+ id: '#1368',
13
+ name: 'Development (1a23b4-MBP)',
14
+ role: '[development]',
15
+ },
16
+ ];
17
+ const color = 'grey';
18
+ const columns = {
19
+ name: {},
20
+ role: {
21
+ color,
22
+ },
23
+ id: {
24
+ color,
25
+ header: 'Identifier',
26
+ },
27
+ };
28
+ const renderInstance = render(React.createElement(Table, { rows: rows, columns: columns }));
29
+ const standard = '';
30
+ const grey = '';
31
+ expect(renderInstance.lastFrame()).toMatchInlineSnapshot(`
32
+ "name role Identifier
33
+ ──────────────────────── ───────────── ──────────
34
+ Dawn ${grey}[live] ${standard} ${grey}#1361 ${standard}
35
+ Studio ${grey} ${standard} ${grey}#1363 ${standard}
36
+ Debut ${grey}[unpublished]${standard} ${grey}#1374 ${standard}
37
+ Development (1a23b4-MBP) ${grey}[development]${standard} ${grey}#1368 ${standard}"
38
+ `);
39
+ });
40
+ });
41
+ //# sourceMappingURL=Table.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Table.test.js","sourceRoot":"","sources":["../../../../../../src/private/node/ui/components/Table/Table.test.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAA;AAE9B,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAC,MAAM,QAAQ,CAAA;AAC7C,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAC,MAAM,EAAC,MAAM,qBAAqB,CAAA;AAE1C,QAAQ,CAAC,OAAO,EAAE,KAAK,IAAI,EAAE;IAC3B,IAAI,CAAC,6BAA6B,EAAE,KAAK,IAAI,EAAE;QAC7C,MAAM,IAAI,GAAiB;YACzB,EAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAC;YAC3C,EAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,EAAC;YACvC,EAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,eAAe,EAAC;YACnD;gBACE,EAAE,EAAE,OAAO;gBACX,IAAI,EAAE,0BAA0B;gBAChC,IAAI,EAAE,eAAe;aACtB;SACF,CAAA;QACD,MAAM,KAAK,GAAG,MAAM,CAAA;QACpB,MAAM,OAAO,GAAG;YACd,IAAI,EAAE,EAAE;YACR,IAAI,EAAE;gBACJ,KAAK;aACN;YACD,EAAE,EAAE;gBACF,KAAK;gBACL,MAAM,EAAE,YAAY;aACrB;SACF,CAAA;QAED,MAAM,cAAc,GAAG,MAAM,CAAC,oBAAC,KAAK,IAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAI,CAAC,CAAA;QAEtE,MAAM,QAAQ,GAAG,OAAO,CAAA;QACxB,MAAM,IAAI,GAAG,OAAO,CAAA;QAEpB,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,CAAC,CAAC,qBAAqB,CAAC;;;kCAG3B,IAAI,gBAAgB,QAAQ,KAAK,IAAI,aAAa,QAAQ;kCAC1D,IAAI,gBAAgB,QAAQ,KAAK,IAAI,aAAa,QAAQ;kCAC1D,IAAI,gBAAgB,QAAQ,KAAK,IAAI,aAAa,QAAQ;kCAC1D,IAAI,gBAAgB,QAAQ,KAAK,IAAI,aAAa,QAAQ;KACvF,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import Table from './Table.js'\nimport ScalarDict from './ScalarDict.js'\nimport {describe, expect, test} from 'vitest'\nimport React from 'react'\nimport {render} from 'ink-testing-library'\n\ndescribe('Table', async () => {\n test('formats the table correctly', async () => {\n const rows: ScalarDict[] = [\n {id: '#1361', name: 'Dawn', role: '[live]'},\n {id: '#1363', name: 'Studio', role: ''},\n {id: '#1374', name: 'Debut', role: '[unpublished]'},\n {\n id: '#1368',\n name: 'Development (1a23b4-MBP)',\n role: '[development]',\n },\n ]\n const color = 'grey'\n const columns = {\n name: {},\n role: {\n color,\n },\n id: {\n color,\n header: 'Identifier',\n },\n }\n\n const renderInstance = render(<Table rows={rows} columns={columns} />)\n\n const standard = '\u001b[39m'\n const grey = '\u001b[90m'\n\n expect(renderInstance.lastFrame()).toMatchInlineSnapshot(`\n \"name role Identifier\n ──────────────────────── ───────────── ──────────\n Dawn ${grey}[live] ${standard} ${grey}#1361 ${standard}\n Studio ${grey} ${standard} ${grey}#1363 ${standard}\n Debut ${grey}[unpublished]${standard} ${grey}#1374 ${standard}\n Development (1a23b4-MBP) ${grey}[development]${standard} ${grey}#1368 ${standard}\"\n `)\n })\n})\n"]}
@@ -1,3 +1,4 @@
1
+ import { TokenItem } from '../../private/node/ui/components/TokenizedText.js';
1
2
  export declare type RandomNameFamily = 'business' | 'creative';
2
3
  /**
3
4
  * Generates a random name by combining an adjective and noun.
@@ -13,6 +14,16 @@ export declare function getRandomName(family?: RandomNameFamily): string;
13
14
  * @returns String with the first letter capitalized.
14
15
  */
15
16
  export declare function capitalize(str: string): string;
17
+ /**
18
+ * Given a list of items, it returns a pluralized string based on the amount of items.
19
+ *
20
+ * @param items - List of items.
21
+ * @param plural - Supplier used when the list of items has more than one item.
22
+ * @param singular - Supplier used when the list of items has a single item.
23
+ * @param none - Supplier used when the list has no items.
24
+ * @returns The {@link TokenItem} supplied by the {@link plural}, {@link singular}, or {@link none} functions.
25
+ */
26
+ export declare function pluralize<T>(items: T[], plural: (items: T[]) => TokenItem, singular: (item: T) => TokenItem, none?: () => TokenItem): TokenItem;
16
27
  /**
17
28
  * Try to convert a string to an int, falling back to undefined if unable to.
18
29
  *
@@ -118,6 +118,27 @@ export function getRandomName(family = 'business') {
118
118
  export function capitalize(str) {
119
119
  return str.substring(0, 1).toUpperCase() + str.substring(1);
120
120
  }
121
+ /**
122
+ * Given a list of items, it returns a pluralized string based on the amount of items.
123
+ *
124
+ * @param items - List of items.
125
+ * @param plural - Supplier used when the list of items has more than one item.
126
+ * @param singular - Supplier used when the list of items has a single item.
127
+ * @param none - Supplier used when the list has no items.
128
+ * @returns The {@link TokenItem} supplied by the {@link plural}, {@link singular}, or {@link none} functions.
129
+ */
130
+ export function pluralize(items, plural, singular, none) {
131
+ if (items.length === 1) {
132
+ return singular(items[0]);
133
+ }
134
+ if (items.length > 1) {
135
+ return plural(items);
136
+ }
137
+ if (none) {
138
+ return none();
139
+ }
140
+ return '';
141
+ }
121
142
  /**
122
143
  * Try to convert a string to an int, falling back to undefined if unable to.
123
144
  *