Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/creds.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { addCredentials, getCredentials, removeCredentials } from './creds';
import { vol } from 'memfs';
import type { StoryblokCredentials } from './types';
// tell vitest to use fs mock from __mocks__ folder
// this can be done in a setup file if fs should always be mocked
vi.mock('node:fs');
Expand All @@ -23,7 +24,7 @@ describe('creds', async () => {
}),
}, '/temp');

const credentials = await getCredentials('/temp/test/credentials.json');
const credentials = await getCredentials('/temp/test/credentials.json') as StoryblokCredentials;

expect(credentials['api.storyblok.com']).toEqual({
login: 'julio.iglesias@storyblok.com',
Expand All @@ -33,7 +34,7 @@ describe('creds', async () => {
});
it('should create a credentials.json file if it does not exist', async () => {
const credentials = await getCredentials('/temp/test/nonexistent.json');
expect(credentials).toEqual({});
expect(credentials).toEqual(null);
});
});

Expand Down
16 changes: 12 additions & 4 deletions src/creds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ import chalk from 'chalk';
import type { RegionCode } from './constants';
import { colorPalette, regionsDomain } from './constants';
import { getStoryblokGlobalPath, readFile, saveToFile } from './utils/filesystem';
import type { StoryblokCredentials } from './types';

export const getCredentials = async (filePath = join(getStoryblokGlobalPath(), 'credentials.json')) => {
export const getCredentials = async (filePath = join(getStoryblokGlobalPath(), 'credentials.json')): Promise<StoryblokCredentials | null> => {
try {
await access(filePath);
const content = await readFile(filePath);
return JSON.parse(content);
const parsedContent = JSON.parse(content);

// Return null if the parsed content is an empty object
if (Object.keys(parsedContent).length === 0) {
return null;
}

return parsedContent;
}
catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
// File doesn't exist, create it with empty credentials
await saveToFile(filePath, JSON.stringify({}, null, 2), { mode: 0o600 });
return {};
return null;
}
handleFileSystemError('read', error as NodeJS.ErrnoException);
return {};
return null;
}
};

Expand Down
9 changes: 5 additions & 4 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function createSession() {
isLoggedIn: false,
};

async function initializeSession(region = 'eu' as RegionCode) {
async function initializeSession() {
// First, check for environment variables
const envCredentials = getEnvCredentials();
if (envCredentials) {
Expand All @@ -30,9 +30,10 @@ function createSession() {
}

// If no environment variables, fall back to .storyblok/credentials.json
const machines = await getCredentials();
const creds = machines[regionsDomain[region] || 'api.storyblok.com'];
if (creds) {
const credentials = await getCredentials();
if (credentials) {
// Todo: evaluate this in future when we want to support multiple regions
const creds = Object.values(credentials)[0];
state.isLoggedIn = true;
state.login = creds.login;
state.password = creds.password;
Expand Down
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { RegionCode } from '../constants';

/**
* Interface representing the default options for a CLI command.
*/
Expand Down Expand Up @@ -54,3 +56,9 @@ export interface FileReaderResult<T> {
data: T[];
error?: Error;
}

export interface StoryblokCredentials {
login: string;
password: string;
region: RegionCode;
}
4 changes: 4 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ export function isRegion(value: RegionCode): value is RegionCode {
return Object.values(regions).includes(value);
}

export function isEmptyObject(obj: object): boolean {
return Object.keys(obj).length === 0;
}

export const isVitest = process.env.VITEST === 'true';
Loading