Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: logger added, console logs replaced, additional logs added (fixes #15) #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ Arguments:
* `content = [{ _id: 'c-05, ... }]` Test content for the course content

### Grunt Commands
```sh
grunt migration:capture # captures current plugins and content
# do plugin/fw updates
grunt migration:migrate # migrates content from capture to new plugins
grunt migration:test # tests the migrations with dummy content
grunt migration:test --file=adapt-contrib-text/migrations/text.js # tests the migrations with dummy content
```
20 changes: 12 additions & 8 deletions api/commands.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import Task from '../lib/Task.js'

export async function load ({ cwd = process.cwd(), scripts = [], cachePath } = {}) {
export async function load ({ cwd = process.cwd(), scripts = [], cachePath, logger } = {}) {
return Task.load({
cwd,
scripts,
cachePath
cachePath,
logger
})
}

export async function capture ({ content, fromPlugins }) {
export async function capture ({ content, fromPlugins, logger }) {
return {
content,
fromPlugins
fromPlugins,
logger
}
};

export async function migrate ({ cwd = process.cwd(), journal }) {
export async function migrate ({ cwd = process.cwd(), journal, logger }) {
return Task.runApplicable({
cwd,
journal
journal,
logger
})
}

export async function test ({ cwd = process.cwd() } = {}) {
export async function test ({ cwd = process.cwd(), logger } = {}) {
return Task.runTests({
cwd,
journal
journal,
logger
})
}
6 changes: 4 additions & 2 deletions api/describe.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Task from '../lib/Task.js'
import Logger from './../lib/Logger.js'

export function describe (description, load) {
console.log('describing', description)
const logger = Logger.getInstance();
logger.info(`Describe -- ${description} -- Registered`)
if (Task.current) {
throw new Error(`Cannot nest describe statements: ${description}`)
logger.error(`Describe -- Cannot nest describe statements -- ${description}`)
}
// eslint-disable-next-line no-new
new Task({ description, load })
Expand Down
4 changes: 4 additions & 0 deletions api/errors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { deferOrRunWrap, successStopOrErrorWrap } from '../lib/lifecycle.js'
import Logger from './../lib/Logger.js'

const logger = Logger.getInstance();

export function throwError (description) {
let error = description
if (!(description instanceof Error)) {
logger.error(`Errors -- ${description}`)
error = new Error(description)
} else {
description = description.message
Expand Down
9 changes: 6 additions & 3 deletions api/tests.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import TaskTest from '../lib/TaskTest.js'
import { deferOrRunWrap } from '../lib/lifecycle.js'
import Logger from '../lib/Logger.js'

const logger = Logger.getInstance();

export function testSuccessWhere (description, {
fromPlugins,
toPlugins,
content
}) {
return deferOrRunWrap(() => {
console.log('testSuccessWhere:', description)
logger.debug(`Tests -- testSuccessWhere ${description}`)
return new TaskTest({
description,
shouldRun: true,
Expand All @@ -24,7 +27,7 @@ export function testStopWhere (description, {
content
}) {
return deferOrRunWrap(() => {
console.log('testStopWhere:', description)
logger.debug(`Tests -- testStopWhere ${description}`)
return new TaskTest({
description,
shouldStop: true,
Expand All @@ -42,7 +45,7 @@ export function testErrorWhere (description, {
content
}) {
return deferOrRunWrap(() => {
console.log('testErrorWhere:', description)
logger.debug(`Tests -- testErrorWhere ${description}`)
return new TaskTest({
description,
shouldError: true,
Expand Down
39 changes: 26 additions & 13 deletions examples/migrations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Example grunt task

module.exports = function (grunt) {
module.exports = function(grunt) {

const Helpers = require('../helpers')(grunt);
const globs = require('globs');
Expand All @@ -27,14 +25,14 @@ module.exports = function (grunt) {
return clone;
}

grunt.registerTask('migration', 'Migrate from on verion to another', function (mode) {
grunt.registerTask('migration', 'Migrate from on verion to another', function(mode) {
const next = this.async();
const buildConfig = Helpers.generateConfigData();
const fileNameIncludes = grunt.option('file');

(async function () {
(async function() {
const migrations = await import('adapt-migrations');

const logger = migrations.Logger.getInstance();
const cwd = process.cwd();
const outputPath = path.join(cwd, './migrations/');
const cache = new migrations.CacheManager();
Expand All @@ -44,7 +42,7 @@ module.exports = function (grunt) {
});

const framework = Helpers.getFramework();
grunt.log.ok(`Using ${framework.useOutputData ? framework.outputPath : framework.sourcePath} folder for course data...`);
logger.debug(`Using ${framework.useOutputData ? framework.outputPath : framework.sourcePath} folder for course data...`);

const plugins = framework.getPlugins().getAllPackageJSONFileItems().map(fileItem => fileItem.item);
const migrationScripts = Array.from(await new Promise(resolve => {
Expand All @@ -59,26 +57,30 @@ module.exports = function (grunt) {

await migrations.load({
cachePath,
scripts: migrationScripts
scripts: migrationScripts,
logger
});

if (mode === 'capture') {

if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath);
const languages = framework.getData().languages.map((language) => language.name);
const languageFile = path.join(outputPath, 'captureLanguages.json');
fs.writeJSONSync(languageFile, languages);
languages.forEach(async (language, index) => {
logger.debug(`Migration -- Capture ${language}`)
const data = framework.getData();
// get all items from config.json file and all language files, append __index__ and __path__ to each item
const content = [
...data.configFile.fileItems,
...data.languages[index].getAllFileItems()
].map(dressPathIndex);
const captured = await migrations.capture({ content, fromPlugins: plugins });
const captured = await migrations.capture({ content, fromPlugins: plugins, logger });
const outputFile = path.join(outputPath, `capture_${language}.json`);
fs.writeJSONSync(outputFile, captured);
});

logger.output(outputPath, 'capture');
return next();
}

Expand All @@ -88,17 +90,19 @@ module.exports = function (grunt) {
const languages = fs.readJSONSync(languagesFile);

for (const language of languages) {
logger.debug(`Migration -- Migrate ${language}`)
const Journal = migrations.Journal;
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath);
const outputFile = path.join(outputPath, `capture_${language}.json`);
const { content, fromPlugins } = fs.readJSONSync(outputFile);
const originalFromPlugins = JSON.parse(JSON.stringify(fromPlugins));
const journal = new Journal({
logger,
data: {
content,
fromPlugins,
originalFromPlugins,
toPlugins: plugins
toPlugins: plugins,
},
supplementEntry: (entry, data) => {
entry._id = data[entry.keys[0]][entry.keys[1]]?._id ?? '';
Expand All @@ -109,8 +113,14 @@ module.exports = function (grunt) {
return entry;
}
});
await migrations.migrate({ journal });
console.log(journal.entries);
await migrations.migrate({ journal, logger });

// Todo - {
// display changes success/failure and request user confirmation before completing
// move saving of content outside of the language loop
// only 1 confirmation per course rather than 1 per language
// output journal entries for revert
// }

// group all content items by path
const outputFilePathItems = _.groupBy(content, '__path__');
Expand All @@ -126,12 +136,14 @@ module.exports = function (grunt) {
const stripped = isSingleObject
? undressPathIndex(outputItems[0]) // config.json, course.json
: outputItems.map(undressPathIndex); // contentObjects.json, articles.json, blocks.json, components.json
// console.log(journal.entries)
fs.writeJSONSync(outputPath, stripped, { replacer: null, spaces: 2 });
});
}
} catch (error) {
console.log(error.stack);
logger.error(error.stack);
}
logger.output(outputPath, 'migrate');
return next();
}

Expand All @@ -143,4 +155,5 @@ module.exports = function (grunt) {
return next();
})();
});

};
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from './api/plugins.js'
import Journal from './lib/Journal.js'
import CacheManager from './lib/CacheManager.js'
import Logger from './lib/Logger.js'

export {
// commands
Expand All @@ -56,5 +57,6 @@ export {
addPlugin,
// environment objects
Journal,
CacheManager
CacheManager,
Logger
}
12 changes: 9 additions & 3 deletions lib/CacheManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import path from 'path'
import globs from 'globs'
import fs from 'fs-extra'
import os from 'os'
import Logger from './Logger.js'

export const ONE_MINUTE = 60 * 1000
export const ONE_HOUR = 60 * ONE_MINUTE
export const ONE_WEEK = 7 * 24 * ONE_HOUR

const logger = Logger.getInstance();

export default class CacheManager {
constructor (maxAge = ONE_WEEK) {
this.maxAge = maxAge
Expand Down Expand Up @@ -49,10 +52,11 @@ export default class CacheManager {

async clean () {
if (!await this.isCleaningTime()) return
logger.debug('CacheManager -- Clean running')
const checkFilePath = await this.getCheckFilePath()
// Touch checkFile
await fs.writeFile(checkFilePath, String(Date.now()))
console.log.ok('Clearing compilation caches...')
logger.debug('CacheManager -- Clearing compilation caches')
const tempPath = await this.getTempPath()
// Fetch all cache files except checkFile
const files = await new Promise((resolve, reject) => globs([
Expand All @@ -70,7 +74,9 @@ export default class CacheManager {
try {
const stat = await fs.stat(file)
age = (now - stat.mtime)
} catch (err) {}
} catch (err) {
logger.error(`CacheManager -- ${err}`)
}
fileAges[index] = { file, age }
}
// Sort by oldest
Expand All @@ -82,7 +88,7 @@ export default class CacheManager {
try {
await fs.unlink(fileAge.file)
} catch (err) {
console.log.warn(`Could not clear cache file ${fileAge.file}`)
logger.error(`CacheManager -- Could not clear cache file ${fileAge.file}`)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/Journal.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default class Journal {
* @param {Function} [options.supplementEntry] Entry supplementation function
*/
constructor ({
logger,
data,
supplementEntry = (entry, data) => entry
}) {
Expand Down
Loading