Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sourcebit-source-filesystem

Package Overview
Dependencies
Maintainers
16
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sourcebit-source-filesystem - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

10

package.json
{
"name": "sourcebit-source-filesystem",
"version": "0.1.3",
"version": "0.1.4",
"description": "Sourcebit filesystem source plugin",

@@ -28,8 +28,8 @@ "main": "src/index.js",

"dependencies": {
"@stackbit/schema": "^0.1.1",
"@stackbit/sdk": "^0.2.16",
"@stackbit/utils": "^0.1.0",
"chokidar": "^3.4.0",
"fs-extra": "^9.0.1",
"lodash": "^4.17.15"
"chokidar": "^3.5.2",
"fs-extra": "^10.0.0",
"lodash": "^4.17.21"
}
}

@@ -5,3 +5,3 @@ const chokidar = require('chokidar');

const _ = require('lodash');
const { loadModels } = require('@stackbit/schema');
const { loadConfig } = require('@stackbit/sdk');
const { parseFile, mapPromise, readDirRecursively } = require('@stackbit/utils');

@@ -36,9 +36,9 @@ const { matchObjectsToModels } = require('./models-matcher');

log('loading stackbit.yaml and models...');
const { models, stackbitYaml } = await loadStackbitYaml(stackbitYamlPath)
setPluginContext({ models, stackbitYaml });
const config = await loadStackbitYaml();
setPluginContext({ config });
// if 'sources' were not specified, use 'pagesDir' and 'dataDir' from stackbit.yaml
if (_.isEmpty(sources)) {
const pagesDir = _.get(stackbitYaml, 'pagesDir', '');
const dataDir = _.get(stackbitYaml, 'dataDir', '');
const pagesDir = _.get(config, 'pagesDir', '');
const dataDir = _.get(config, 'dataDir', '');
sources = [

@@ -70,4 +70,4 @@ { name: 'pages', path: pagesDir },

log('reloading stackbit.yaml and models...');
const { models, stackbitYaml } = await loadStackbitYaml(stackbitYamlPath)
setPluginContext({ models, stackbitYaml });
const config = await loadStackbitYaml();
setPluginContext({ config });
}

@@ -105,8 +105,8 @@

let objects = context.files;
if (context.stackbitYaml && context.models) {
objects = matchObjectsToModels(context.files, context.models, {
if (context.config) {
objects = matchObjectsToModels(context.files, context.config.models, {
pageObjectsPredicate: _.matches({__metadata: {sourceName: 'pages'}}),
dataObjectsPredicate: _.matches({__metadata: {sourceName: 'data'}}),
pageLayoutKey: ['frontmatter', _.get(context, 'stackbitYaml.pageLayoutKey', 'layout')],
dataTypeKey: _.get(context, 'stackbitYaml.objectTypeKey', 'type'),
pageLayoutKey: ['frontmatter', _.get(context.config, 'pageLayoutKey', 'layout')],
dataTypeKey: _.get(context.config, 'objectTypeKey', 'type'),
objectIdKeyPath: '__metadata.id',

@@ -124,8 +124,11 @@ objectFileKeyPath: '__metadata.relSourcePath',

async function loadStackbitYaml(stackbitYamlPath) {
log('loading stackbit.yaml...');
const stackbitYaml = await parseFile(stackbitYamlPath);
const models = loadModels(stackbitYaml.models);
log(`loaded stackbit.yaml, found ${models.length} models`);
return { models, stackbitYaml };
async function loadStackbitYaml() {
const cwd = process.cwd();
const { config } = await loadConfig({ dirPath: cwd });
if (!config) {
log('failed to load stackbit.yaml');
return null;
}
log(`loaded stackbit.yaml, found ${config.models.length} models`);
return config;
}

@@ -132,0 +135,0 @@

const path = require('path');
const _ = require('lodash');
const { getModelByQuery } = require('@stackbit/schema');
const { getModelByQuery } = require('@stackbit/sdk');

@@ -10,2 +10,10 @@

function log(message) {
console.log(`[sourcebit-source-filesystem] ${message}`);
}
function logError(message) {
console.error(`[sourcebit-source-filesystem] ${message}`);
}
function matchObjectsToModels(objects, models, { pageObjectsPredicate, dataObjectsPredicate, pageLayoutKey, dataTypeKey, objectIdKeyPath, objectFileKeyPath, source, mergeDataModels, logMatchedModels }) {

@@ -16,34 +24,28 @@ const modelsByName = _.keyBy(models, 'name');

console.log('matching files to models...');
log('matching files to models...');
const modelsByObjectIds = _.reduce(objects, (accum, object) => {
if (pageObjectsPredicate(object)) {
// return matchPageModel(object, pageModels, modelsByName);
let model;
try {
model = getModelByQuery({
filePath: _.get(object, objectFileKeyPath),
type: _.get(object, pageLayoutKey),
modelTypeKeyPath: 'layout'
}, pageModels);
} catch (err) {
console.error(err.message);
return accum;
const { model, error } = getModelByQuery({
filePath: _.get(object, objectFileKeyPath),
type: _.get(object, pageLayoutKey),
modelTypeKeyPath: 'layout'
}, pageModels);
if (error) {
logError(error.message);
} else {
const objectId = _.get(object, objectIdKeyPath);
accum[objectId] = model;
}
const objectId = _.get(object, objectIdKeyPath);
accum[objectId] = model;
} else if (dataObjectsPredicate(object)) {
// return matchDataModel(object, dataModelsByFilePath, modelsByName);
let model;
try {
model = getModelByQuery({
filePath: _.get(object, objectFileKeyPath),
type: _.get(object, dataTypeKey),
modelTypeKeyPath: 'name'
}, dataModels);
} catch (err) {
console.error(err.message);
return accum;
const { model, error } = getModelByQuery({
filePath: _.get(object, objectFileKeyPath),
type: _.get(object, dataTypeKey),
modelTypeKeyPath: 'name'
}, dataModels);
if (error) {
logError(error.message);
} else {
const objectId = _.get(object, objectIdKeyPath);
accum[objectId] = model;
}
const objectId = _.get(object, objectIdKeyPath);
accum[objectId] = model;
}

@@ -53,7 +55,7 @@ return accum;

console.log(`${_.size(modelsByObjectIds)} of ${_.size(objects)} files were matched to models`);
log(`${_.size(modelsByObjectIds)} of ${_.size(objects)} files were matched to models`);
if (logMatchedModels) {
const modelNames = _.uniq(_.map(modelsByObjectIds, 'name'));
const longestModelName = _.maxBy(modelNames, _.size);
console.log('matched models:' + _.map(modelsByObjectIds, (model, objectId) => '\n ' + _.padEnd(model.name, longestModelName.length) + ' : ' + objectId).join(''));
log('matched models:' + _.map(modelsByObjectIds, (model, objectId) => '\n ' + _.padEnd(model.name, longestModelName.length) + ' : ' + objectId).join(''));
}

@@ -86,3 +88,3 @@

if (!_.isPlainObject(data)) {
console.error(`value must be an object, ${location}`);
logError(`value must be an object, ${location}`);
return data;

@@ -125,3 +127,3 @@ }

if (!_.has(fieldModel, 'models') || !_.isArray(fieldModel.models) || fieldModel.models.length < 1) {
console.error(`field of type 'model' must have 'models' property having an array with at least one model name, ${location}`);
logError(`field of type 'model' must have 'models' property with array having at least one model name, ${location}`);
return fieldValue;

@@ -133,3 +135,3 @@ }

if (!_.has(modelsByName, modelName)) {
console.error(`the values of the 'models' array of the field of type 'model' must be the names of existing models, ${location}`);
logError(`the 'models' array of the field of type 'model' must include the names of existing models, ${location}`);
return fieldValue;

@@ -139,3 +141,3 @@ }

if (!_.has(fieldValue, 'type')) {
console.error(`object referenced by a field of type 'model' having more than one model in 'models' array, must have 'type' property specifying the name of the object's model, ${location}`);
logError(`object referenced by a field of type 'model' having more than one model in 'models' array, must have 'type' property specifying the name of the object's model, ${location}`);
return fieldValue;

@@ -145,3 +147,3 @@ }

if (!_.has(modelsByName, modelName)) {
console.error(`the value of the 'type' property of the object referenced by a field of type 'model' must be the name of an existing model, ${location}`);
logError(`the value of the 'type' property of the object referenced by a field of type 'model' must be the name of an existing model, ${location}`);
return fieldValue;

@@ -160,3 +162,3 @@ }

if (!_.isArray(fieldValue)) {
console.error(`the value referenced by a field of type 'list' must be an array, ${location}`);
logError(`the value referenced by a field of type 'list' must be an array, ${location}`);
return fieldValue;

@@ -163,0 +165,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc