Skip to content

Commit

Permalink
begin work on RefreshType and ability to switch soft/hard delete on f…
Browse files Browse the repository at this point in the history
…olders; write real comments for old temp stuff
  • Loading branch information
mikaelvesavuori committed Sep 4, 2021
1 parent e486f28 commit fdfaccd
Show file tree
Hide file tree
Showing 162 changed files with 14,646 additions and 1,478 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Generated files
.figmagicrc
figmagic.json
/.figmagic
/.figmagic*
/.webpack
/figma
/grid
Expand Down
4 changes: 2 additions & 2 deletions __tests__/frameworks/filesystem/refresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { refresh } from '../../../bin/frameworks/filesystem/refresh';
describe('Failure cases', () => {
test('It should throw an error if no argument is provided', async () => {
// @ts-ignore
await expect(() => refresh()).rejects.toThrow();
await expect(() => refresh()).toThrow();
});
});

Expand All @@ -15,7 +15,7 @@ describe('Success cases', () => {
const testPath = `__test-refresh__`;
await trash([`./${testPath}`]);

await refresh(testPath);
refresh(testPath, 'hard');

const FILE_EXISTS = fs.existsSync(testPath);
expect(FILE_EXISTS).toBeTruthy();
Expand Down
4 changes: 3 additions & 1 deletion __tests__/frameworks/filesystem/writeBaseJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ describe('Success cases', () => {
test('It should write the base Figma file', async () => {
const testFile = '__test-writeBaseJson.txt';
const path = `${TEST_FOLDER}/${testFile}`;
await writeBaseJson(TEST_FOLDER, testFile, { data: 'something' });
await writeBaseJson({ figmagicFolder: TEST_FOLDER, refreshType: 'hard' }, testFile, {
data: 'something'
});
const fileContent = fs.readFileSync(path, { encoding: 'utf-8' });
expect(fileContent).toBe(`{"data":"something"}`);
});
Expand Down
12 changes: 12 additions & 0 deletions bin/contracts/Refresh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type RefreshConfig = {
figmagicFolder: string;
/**
* @description Sets how a file is refreshed. In 'soft' mode, the previous folder is
* renamed and placed in a Figmagic-created trash folder with a ISO 8601 timestamp.
* In 'hard' mode, Node's `fs` module is used to erase the folder with no way of
* retrieving the old folder.
*/
refreshType: RefreshType;
};

export type RefreshType = 'soft' | 'hard';
2 changes: 1 addition & 1 deletion bin/entities/Token/logic/makeOpacityTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function makeOpacityTokens(
const opacityTokens = TOKENS.reduce((tokens: { [index: string]: any }, item: Frame) => {
if (!item.name) throw Error(ErrorMakeOpacityTokensMissingProps);

// Note: Figma API does not provide an opacity value if its 100%. We will assume it defaults to 1 if undefined.
// Note: Figma API does not provide an opacity value if it's 100%. We will assume it defaults to 1 if undefined.
const NAME = camelize(item.name);
const OPACITY = (() => {
let opacity: string | number = 1;
Expand Down
34 changes: 28 additions & 6 deletions bin/frameworks/filesystem/refresh.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
//import trash from 'trash';
import fs from 'fs';
import path from 'path';

import { RefreshType } from '../../contracts/Refresh';

import { createFolder } from './createFolder';

import { ErrorRefresh } from '../errors/errors';

/**
* @description Refresh a folder by trashing it first, then creating a new folder
* @description Refresh a folder by first either deleting it or putting it in a local trash folder, then creating a new folder.
*/
//@ts-ignore
export async function refresh(path: string, trashExistingFolder = true): Promise<void> {
export function refresh(
folderPath: string,
refreshType: RefreshType,
trashExistingFolder = true
): void {
try {
if (!path) throw Error(ErrorRefresh);
if (!folderPath) throw Error(ErrorRefresh);

//if (trashExistingFolder) await trash([`./${path}`]); // TODO
createFolder(path);
if (trashExistingFolder && fs.existsSync(folderPath)) {
// Soft erase by moving into trash folder, giving the old copy a UTC (ISO 8601) timestamp
if (refreshType === 'soft') {
createFolder('.figmagic-trash');
const date = new Date();
fs.renameSync(
path.resolve(process.cwd(), folderPath),
`.figmagic-trash/${folderPath}_${date.getUTCFullYear()}-${date.getUTCDate()}-${date.getUTCMonth()} ${date.getUTCHours()}:${date.getUTCMinutes()}:${date.getUTCSeconds()}`
);
} // Hard erase of existing folder
else if (refreshType === 'hard') {
const NODE_VERSION = process.versions.node;
if (NODE_VERSION > '14.14.0')
fs.rmSync(path.resolve(process.cwd(), folderPath), { recursive: true });
}
}
createFolder(folderPath);
} catch (error: any) {
throw Error(error);
}
Expand Down
8 changes: 5 additions & 3 deletions bin/frameworks/filesystem/writeBaseJson.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FigmaData } from '../../contracts/FigmaData';
import { RefreshConfig } from '../../contracts/Refresh';

import { refresh } from './refresh';
import { write } from './write';
Expand All @@ -10,15 +11,16 @@ import { ErrorWriteBaseJson } from '../errors/errors';
* @description Write base Figma JSON document to disk
*/
export async function writeBaseJson(
figmagicFolder: string,
refreshConfig: RefreshConfig,
figmaData: string,
data: FigmaData | Record<string, unknown>
): Promise<void> {
if (!figmagicFolder || !figmaData || !data) throw Error(ErrorWriteBaseJson);
if (!refreshConfig || !figmaData || !data) throw Error(ErrorWriteBaseJson);

console.log(MsgWriteBaseFile);
try {
await refresh(figmagicFolder);
const { figmagicFolder, refreshType } = refreshConfig;
refresh(figmagicFolder, refreshType);
write(`${figmagicFolder}/${figmaData}`, JSON.stringify(data));
} catch (error: any) {
throw Error(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ErrorCheckIfStringOnlyContainsReturnsOrSpaces } from '../../../bin/frameworks/errors/errors';

/**
* @description TODO
* @description Helper to see if a string has no actual content, i.e. only returns or spaces.
*/
export function checkIfStringOnlyContainsReturnsOrSpaces(str: string): boolean {
if (!str) throw Error(ErrorCheckIfStringOnlyContainsReturnsOrSpaces);
Expand Down
2 changes: 1 addition & 1 deletion bin/frameworks/string/removeAllIds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @description TODO
* @description Replace all IDs in the shape "__#{something}" from a given string, exchanging them with spaces.
*/
export function removeAllIds(str: string): string {
return str.replace(/__#(.*?) /gi, ' ');
Expand Down
2 changes: 1 addition & 1 deletion bin/usecases/createElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function createElements(config: Config, data: FigmaData): Promise<v
}

try {
await refresh(config.outputFolderElements, false);
refresh(config.outputFolderElements, 'soft', false); // TODO: Add to configuration
const { components }: any = data;
handleElements({
children: data.document.children,
Expand Down
2 changes: 1 addition & 1 deletion bin/usecases/createGraphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function createGraphics(config: Config, data: FigmaData): Promise<v
if (!config || !data) throw Error(ErrorCreateGraphics);
console.log(MsgSyncGraphics);

await refresh(config.outputFolderGraphics);
refresh(config.outputFolderGraphics, 'soft'); // TODO: Add to configuration

const graphicsPage = createPage(data.document.children, 'Graphics');
const fileList = await processGraphics(graphicsPage, config);
Expand Down
2 changes: 1 addition & 1 deletion bin/usecases/createTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function createTokens(config: Config, data: FigmaData): Promise<voi
if (!config || !data) throw Error(ErrorCreateTokens);
console.log(MsgWriteTokens);

await refresh(config.outputFolderTokens);
refresh(config.outputFolderTokens, 'soft'); // TODO: Add to configuration

const tokensPage: Frame[] = createPage(data.document.children, 'Design Tokens');
const processedTokens = processTokens(tokensPage, config);
Expand Down
5 changes: 5 additions & 0 deletions build/bin/contracts/Refresh.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare type RefreshConfig = {
figmagicFolder: string;
refreshType: RefreshType;
};
export declare type RefreshType = 'soft' | 'hard';
3 changes: 2 additions & 1 deletion build/bin/frameworks/filesystem/refresh.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export declare function refresh(path: string, trashExistingFolder?: boolean): Promise<void>;
import { RefreshType } from '../../contracts/Refresh';
export declare function refresh(folderPath: string, refreshType: RefreshType, trashExistingFolder?: boolean): void;
3 changes: 2 additions & 1 deletion build/bin/frameworks/filesystem/writeBaseJson.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import { FigmaData } from '../../contracts/FigmaData';
export declare function writeBaseJson(figmagicFolder: string, figmaData: string, data: FigmaData | Record<string, unknown>): Promise<void>;
import { RefreshConfig } from '../../contracts/Refresh';
export declare function writeBaseJson(refreshConfig: RefreshConfig, figmaData: string, data: FigmaData | Record<string, unknown>): Promise<void>;
2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/search.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ <h3>Want to add or rethink something in Figmagic?</h3>
<li class=" tsd-kind-module">
<a href="modules/contracts_ProcessedToken.html">contracts/<wbr>Processed<wbr>Token</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/contracts_Refresh.html">contracts/<wbr>Refresh</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/contracts_Templates.html">contracts/<wbr>Templates</a>
</li>
Expand Down
4 changes: 2 additions & 2 deletions docs/interfaces/contracts_css.css.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ <h3>css</h3>
<div class="tsd-signature tsd-kind-icon">css<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/mikaelvesavuori/figmagic/blob/0b568667/bin/contracts/Css.ts#L2">contracts/Css.ts:2</a></li>
<li>Defined in <a href="https://github.com/mikaelvesavuori/figmagic/blob/e486f28f/bin/contracts/Css.ts#L2">contracts/Css.ts:2</a></li>
</ul>
</aside>
</section>
Expand All @@ -108,7 +108,7 @@ <h3>imports</h3>
<div class="tsd-signature tsd-kind-icon">imports<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">[]</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/mikaelvesavuori/figmagic/blob/0b568667/bin/contracts/Css.ts#L3">contracts/Css.ts:3</a></li>
<li>Defined in <a href="https://github.com/mikaelvesavuori/figmagic/blob/e486f28f/bin/contracts/Css.ts#L3">contracts/Css.ts:3</a></li>
</ul>
</aside>
</section>
Expand Down
4 changes: 2 additions & 2 deletions docs/interfaces/contracts_css.processedselfnamedcss.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ <h3>updated<wbr>Css</h3>
<div class="tsd-signature tsd-kind-icon">updated<wbr>Css<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/mikaelvesavuori/figmagic/blob/0b568667/bin/contracts/Css.ts#L7">contracts/Css.ts:7</a></li>
<li>Defined in <a href="https://github.com/mikaelvesavuori/figmagic/blob/e486f28f/bin/contracts/Css.ts#L7">contracts/Css.ts:7</a></li>
</ul>
</aside>
</section>
Expand All @@ -108,7 +108,7 @@ <h3>updated<wbr>Imports</h3>
<div class="tsd-signature tsd-kind-icon">updated<wbr>Imports<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">[]</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/mikaelvesavuori/figmagic/blob/0b568667/bin/contracts/Css.ts#L8">contracts/Css.ts:8</a></li>
<li>Defined in <a href="https://github.com/mikaelvesavuori/figmagic/blob/e486f28f/bin/contracts/Css.ts#L8">contracts/Css.ts:8</a></li>
</ul>
</aside>
</section>
Expand Down
Loading

0 comments on commit fdfaccd

Please sign in to comment.