Skip to content
Open
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
218 changes: 145 additions & 73 deletions src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,153 @@ import {
updateComponentFile,
updateComponentConfig,
} from './actions/components.js';
import { validateCampaignSass, validateComponent } from './actions/validate.js';
import { getToken } from './actions/auth.js';
import { loadConfig } from './config.js';

function startLoader(message, oraImpl = ora) {
return oraImpl(message).start();
}

export async function handleCampaignChange(
filenameRaw,
{
campaignsDir,
token,
uploadStylesFn = uploadStyles,
validateCampaignSassFn = validateCampaignSass,
oraFn = ora,
} = {}
) {
const relative = path.relative(campaignsDir, filenameRaw);
const parts = relative.split(path.sep);
// Only handle stylesheet changes: <campaign-path>/stylesheets/...
if (parts.length < 3 || parts[1] !== 'stylesheets') return;
const campaignPath = parts[0];
const loader = startLoader(`Saving ${relative}`, oraFn);
const validation = await validateCampaignSassFn({
campaign: campaignPath,
token,
});
if (!validation.ok) {
loader.fail(validation.error);
return;
}
await uploadStylesFn(campaignPath);
loader.succeed();
}

export async function handleComponentChange(
filenameRaw,
{
componentsDir,
config,
fsModule = fs,
pathModule = path,
updateComponentFileFn = updateComponentFile,
updateComponentConfigFn = updateComponentConfig,
validateComponentFn = validateComponent,
oraFn = ora,
errorFn = error,
} = {}
) {
const filename = pathModule.relative(componentsDir, filenameRaw);
const componentName = filename.split(pathModule.sep)[0];
if (!componentName) return;
const loader = startLoader(`Saving ${filename}`, oraFn);
const validation = await validateComponentFn({ name: componentName });
if (!validation.ok) {
loader.fail(validation.error);
return;
}

try {
if (filename.includes('.json')) {
await updateComponentConfigFn(
{
filename,
file: fsModule.readFileSync(
pathModule.join(
componentsDir,
filename.replace('.json', '.js')
),
'utf8'
),
config: JSON.parse(
fsModule.readFileSync(pathModule.join(componentsDir, filename), 'utf8')
),
},
config
);
} else {
await updateComponentFileFn(
{
filename,
file: fsModule.readFileSync(pathModule.join(componentsDir, filename), 'utf8'),
config: JSON.parse(
fsModule.readFileSync(
pathModule.join(
componentsDir,
filename.replace('.js', '.json')
),
'utf8'
)
),
},
config
);
}
} catch (e) {
return errorFn(e, loader);
}
Comment thread
KeinerM marked this conversation as resolved.

loader.succeed();
}

export function registerStartWatchers(
{
campaignsDir,
componentsDir,
config,
watchFn = watch,
} = {},
dependencies = {}
) {
const campaignWatcher = watchFn(
campaignsDir,
{ encoding: 'utf8', recursive: true },
async (eventType, filenameRaw) => {
await handleCampaignChange(filenameRaw, {
campaignsDir,
token: config.token,
uploadStylesFn: dependencies.uploadStylesFn,
validateCampaignSassFn: dependencies.validateCampaignSassFn,
oraFn: dependencies.oraFn,
});
}
);

const componentWatcher = watchFn(
componentsDir,
{ encoding: 'utf8', recursive: true },
async (eventType, filenameRaw) => {
await handleComponentChange(filenameRaw, {
componentsDir,
config,
fsModule: dependencies.fsModule,
pathModule: dependencies.pathModule,
updateComponentFileFn: dependencies.updateComponentFileFn,
updateComponentConfigFn: dependencies.updateComponentConfigFn,
validateComponentFn: dependencies.validateComponentFn,
oraFn: dependencies.oraFn,
errorFn: dependencies.errorFn,
});
}
);

return { campaignWatcher, componentWatcher };
}

export default async function start() {
const layout = detectLayout(process.cwd());
if (shouldRefuseLayoutForCommand('start', layout)) {
Expand Down Expand Up @@ -60,77 +204,5 @@ export default async function start() {
// watch folders
const campaignsDir = path.join(process.cwd(), 'campaigns');
const componentsDir = path.join(process.cwd(), 'components');
watch(
campaignsDir,
{ encoding: 'utf8', recursive: true },
async (eventType, filenameRaw) => {
const relative = path.relative(campaignsDir, filenameRaw);
const parts = relative.split(path.sep);
// Only handle stylesheet changes: <campaign-path>/stylesheets/...
if (parts.length < 3 || parts[1] !== 'stylesheets') return;
const campaignPath = parts[0];
const loader = ora(`Saving ${relative}`).start();

await uploadStyles(campaignPath);

loader.succeed();
}
);

watch(
componentsDir,
{ encoding: 'utf8', recursive: true },
async (eventType, filenameRaw) => {
const filename = path.relative(componentsDir, filenameRaw);
const loader = ora(`Saving ${filename}`).start();

try {
if (filename.includes('.json')) {
await updateComponentConfig(
{
filename,
file: fs.readFileSync(
path.join(
componentsDir,
filename.replace('.json', '.js')
),
'utf8'
),
config: JSON.parse(
fs.readFileSync(
path.join(componentsDir, filename),
'utf8'
)
),
},
config
);
} else {
const result = await updateComponentFile(
{
filename,
file: fs.readFileSync(
path.join(componentsDir, filename),
'utf8'
),
config: JSON.parse(
fs.readFileSync(
path.join(
componentsDir,
filename.replace('.js', '.json')
),
'utf8'
)
),
},
config
);
}
} catch (e) {
return error(e, loader);
}

loader.succeed();
}
);
registerStartWatchers({ campaignsDir, componentsDir, config });
}
Loading